Python PIP
The standard library is large, but the real superpower of Python is its ecosystem: more than half a million third-party packages on the Python Package Index (PyPI, pypi.org). pip is the tool that downloads and installs them.
Note: pip runs in your computer's terminal, not inside a Python program. The terminal sessions on this page are simulated — the output shown is typical, but versions and sizes will differ on your machine. (In the browser playgrounds, Pyodide can load many popular packages such as NumPy automatically when you import them.)
Is pip installed?
pip ships with every modern Python installer. Check its version:
python3 -m pip --versionpip 24.0 from /usr/lib/python3/dist-packages/pip (python 3.12)
Tip: Prefer
python3 -m pip(orpy -m pipon Windows) over a barepipcommand. It guarantees you install into the same Python that you run, which avoids the classic "I installed it but Python can't find it" problem.
Installing a package
python3 -m pip install requestsCollecting requests
Downloading requests-2.32.3-py3-none-any.whl (64 kB)
Collecting urllib3<3,>=1.21.1
Downloading urllib3-2.2.2-py3-none-any.whl (121 kB)
Collecting idna<4,>=2.5
Downloading idna-3.7-py3-none-any.whl (66 kB)
Installing collected packages: urllib3, idna, requests
Successfully installed idna-3.7 requests-2.32.3 urllib3-2.2.2
pip also installed requests' dependencies automatically. Afterwards the package is imported like any module:
import requests
response = requests.get("https://api.github.com")
print(response.status_code)Common pip commands
| Command | What it does |
|---|---|
pip install name |
install the latest version |
pip install name==2.1.0 |
install an exact version |
pip install "name>=2,<3" |
install within a version range |
pip install --upgrade name |
upgrade to the newest version |
pip uninstall name |
remove a package |
pip list |
list installed packages |
pip show name |
details: version, location, dependencies |
pip freeze > requirements.txt |
save exact versions to a file |
pip install -r requirements.txt |
install everything listed in a file |
python3 -m pip show requestsName: requests
Version: 2.32.3
Summary: Python HTTP for Humans.
Home-page: https://requests.readthedocs.io
License: Apache-2.0
Location: /home/you/project/.venv/lib/python3.12/site-packages
Requires: certifi, charset-normalizer, idna, urllib3
Required-by:
requirements.txt
Projects record their dependencies in a requirements.txt file so anyone can recreate the same setup:
requests==2.32.3
rich>=13.0
python-dateutil~=2.9python3 -m pip install -r requirements.txtCollecting requests==2.32.3
Collecting rich>=13.0
Collecting python-dateutil~=2.9
...
Successfully installed python-dateutil-2.9.0 requests-2.32.3 rich-13.7.1
Newer projects often declare dependencies in a pyproject.toml file instead, but requirements.txt remains very common.
Checking what's installed from Python
Your code can ask which version of an installed package it's running with, using importlib.metadata (Python 3.8+). metadata.version("requests") returns a version string such as "2.32.3", and a missing package raises PackageNotFoundError:
from importlib import metadata
def installed(package):
try:
metadata.version(package)
return True
except metadata.PackageNotFoundError:
return False
print(installed("surely-not-a-real-package-xyz"))False
Version specifiers
| Specifier | Meaning |
|---|---|
==1.4.2 |
exactly 1.4.2 |
>=1.4 |
1.4 or newer |
<2 |
anything below 2.0 |
~=1.4.2 |
compatible: >=1.4.2, <1.5 |
!=1.5.0 |
anything except 1.5.0 |
Versions usually follow semantic versioning: MAJOR.MINOR.PATCH. A major bump may break your code; minor and patch bumps shouldn't.
Installing into the right place
Running pip install with your system Python installs packages globally, where different projects can conflict ("project A needs Django 4, project B needs Django 5"). On many Linux distributions pip even refuses, with an "externally-managed-environment" error. The fix is a virtual environment — a private package folder per project. That's the topic of the Virtual Environments lesson.
python3 -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
python -m pip install requestsTip: Only install packages you trust. Check the package name carefully — attackers sometimes publish look-alike names such as
reqeusts.
Test yourself
What is PyPI?
- Python's built-in debugger
- The Python Package Index, where pip downloads packages from
- A faster Python interpreter
- A PyPy plugin
Which command saves the exact versions of installed packages to a file?
- pip save
- pip list --save
- pip freeze > requirements.txt
- pip export
Why is python3 -m pip install x recommended over pip install x?
- It's faster
- It ensures pip installs into the same Python interpreter you run
- It installs offline
- It skips dependencies