Lesson 14 of 16 · Project Setup
Installing Packages With pip
Learn to install, upgrade and remove Python packages with pip, pin versions, and share your project's dependencies with a requirements.txt file.
- 9 min read
- Intermediate
- Reviewed
- What a package is and where pip gets it from
- How to install, upgrade, list and remove packages
- How version numbers and version rules work
- How to share a project's packages with requirements.txt
Packages and PyPI
Python comes with a large standard library: json, csv, datetime and many more. Everything else is a package written by someone else and published on the Python Package Index, known as PyPI (pypi.org). It holds hundreds of thousands of packages, including NumPy, pandas, PyTorch and the official clients for AI APIs.
pip is the tool that downloads packages from PyPI and installs them into your environment. It comes with Python.
Installing a package
python -m pip install requestspip downloads requests, plus any packages that requests itself needs, and installs them. Now your code can use it:
import requests
response = requests.get("https://api.github.com")
print(response.status_code)Writing python -m pip instead of plain pip makes sure you use the pip that belongs to the active Python. Both usually work inside an activated environment, but python -m pip never surprises you.
The everyday commands
| Command | What it does |
|---|---|
python -m pip install pandas | Install a package |
python -m pip install pandas numpy | Install several at once |
python -m pip install --upgrade pandas | Upgrade to the newest version |
python -m pip list | Show everything installed |
python -m pip show pandas | Details of one package, including its version |
python -m pip uninstall pandas | Remove a package |
Version numbers
Most packages use three numbers: major.minor.patch, such as 2.1.3.
- Patch (
2.1.3to2.1.4): bug fixes only. - Minor (
2.1to2.2): new features, old code keeps working. - Major (
2to3): big changes that may break your code.
You can ask pip for a particular version, or a range:
python -m pip install "pandas==2.2.3" # exactly this version
python -m pip install "pandas>=2.2" # this version or newer
python -m pip install "pandas~=2.2.0" # 2.2.x only: patches, no new featuresThe version numbers here are only examples. Use quotation marks around the requirement, because some terminals treat > and < as special characters.
Is a package installed?
From Python, importlib.util.find_spec() tells you whether a module can be imported, without importing it.
import importlib.util
for name in ["json", "csv", "velamuvi_not_a_real_package"]:
found = importlib.util.find_spec(name) is not None
print(f"{name}: {'available' if found else 'not installed'}")json and csv are in the standard library, so they are always available. The made-up name is not installed anywhere.
Sharing your packages: requirements.txt
When someone else (or you, on another computer) wants to run your project, they need the same packages. The convention is a file called requirements.txt that lists them, one per line.
Create it from your current environment:
python -m pip freeze > requirements.txtpip freeze prints every installed package with its exact version, and > saves that list into the file. It looks something like this:
certifi==2025.8.3
charset-normalizer==3.4.3
idna==3.10
requests==2.32.5
urllib3==2.5.0Anyone can then recreate the same environment in two steps:
python -m venv .venv
# activate it, then:
python -m pip install -r requirements.txtUnlike the .venv folder, requirements.txt is small and belongs in Git.
Common mistakes
Read a small requirements.txt and show what each line asks for.
requirements = """ numpy==2.1.0 pandas>=2.2 # testing pytest """ # Print each package and its version rule, one per line. # Skip blank lines and comment lines (they start with #). # A package without a rule should say "any version". # Output must be exactly: # numpy: ==2.1.0 # pandas: >=2.2 # pytest: any version
What does python -m pip install -r requirements.txt do?
Summary
- Packages live on PyPI, and pip installs them into the active environment.
- Use
python -m pip install,--upgrade,list,showanduninstall. - Versions follow major.minor.patch. Pin them with
==, or allow a range with>=or~=. pip freeze > requirements.txtrecords your packages, andpip install -r requirements.txtrecreates them.
Next: organising a project into files and folders.
Finished reading? Tick it off to track your progress.