Published , Last updated
If you're working with Python, managing dependencies is crucial for maintaining clean and reproducible environments. Two essential tools for this are pip
, Python’s package installer, and requirements.txt
, a file that helps you install multiple packages at once.
A requirements.txt
file is a plain text file used in Python projects to list all the dependencies (packages and their versions) that the project needs to run. It allows developers to install all required packages with a single command using pip
.
Here, I’ll cover how to install packages with pip and how to use and write a requirements.txt
file. Whether you’re a beginner or looking to automate your Python workflows, this post is for you.
pip
is the default package installer for Python. It allows you to install packages from the Python Package Index (PyPI) and other repositories. If you're using Python 3.4 and above, pip is already included.
pip install package-name # Install a package
pip uninstall package-name # Uninstall a package
pip freeze # List installed packages
pip install -r requirements.txt # Install from a requirements file
Installing a single package is simple. For example, to install requests
, run:
pip install requests
To install a specific version:
pip install requests==2.28.1
To upgrade a package:
pip install --upgrade requests
You can also install packages globally or inside a virtual environment (recommended for projects). To create a virtual environment:
python -m venv venv
source venv/bin/activate # Linux/Mac
venv\Scripts\activate # Windows
Then install your packages using pip within this environment.
requirements.txt
A requirements.txt
file lists all your Python project's dependencies. This makes it easier for others (or future you) to set up the project with a single command.
To generate a requirements.txt
from your current environment:
pip freeze > requirements.txt
This creates a file like:
requests==2.28.1
flask==2.2.5
numpy==1.24.3
Each line contains a package name and its version. You can edit this file manually or keep it generated via pip freeze
.
You can also write it yourself:
requests
flask>=2.0
numpy==1.23.0
This allows flexibility:
Operator | Meaning |
---|---|
~= | Compatible release clause (e.g., ~=1.4 means >=1.4, <2.0 ) |
== | Exact version match |
!= | Exclude specific version(s) |
<= , >= | Inclusive comparison |
< , > | Exclusive comparison |
=== | Arbitrary (often exact string) equality |
You can even include packages from GitHub or private URLs:
git+https://github.com/psf/requests.git
Or point to local directories or wheels:
-e ./my-local-package/
Once you have a requirements.txt
, anyone can install all dependencies with:
pip install -r requirements.txt
This ensures a consistent environment across different machines or deployments.
Reference document: https://www.python.org/dev/peps/pep-0440/#version-specifiers
To update your dependencies:
Upgrade your packages manually or with a tool like pip-review
:
pip install pip-review
pip-review --auto
Regenerate requirements.txt
:
pip freeze > requirements.txt
requirements.txt
– for productiondev-requirements.txt
– for testing, linters, etc.==
) to avoid unexpected issues when packages update.requirements.txt
with pip freeze
.
Using pip
and requirements.txt
effectively can save you time, avoid dependency issues, and make your Python projects more shareable and reproducible. Whether you’re working on a small script or a large app, managing packages smartly is a must.