How to Create Requirements.txt File in Python
A requirements.txt file is a text file that lists all the Python dependencies required for a project. It enables users to install all dependencies quickly with a single command, ensuring consistency across different environments.
1. Why Use a requirements.txt File?
- Reproducibility: All developers and production environments will be working on the same versions of packages.
- Portability: It allows the easy transfer of the project to other machines.
- Deployment: It supports deployment of projects on cloud platforms or servers with the required dependencies.
- Collaboration: Allows team members to install all dependencies quickly.
2. Creating a requirements.txt File
Method 1: Manually Creating requirements.txt
You can manually create and write dependencies in requirements.txt.
Steps:
- Open a terminal (Command Prompt, PowerShell, or Bash).
- Create the file using a text editor:
- Windows:
notepad requirements.txt
- Linux/macOS:
nano requirements.txt
3. Add package names and versions (one per line):
numpy==1.21.2
pandas>=1.3.0
flask<=2.0.0
requests
==→ Exact version (e.g.,numpy==1.21.2)>=→ Minimum version (e.g.,pandas>=1.3.0)<=→ Maximum version (e.g.,flask<=2.0.0)
4. Save the file.
Method 2: Automatically Generating requirements.txt
If you have already installed the required packages, you can generate requirements.txt automatically.
Using pip freeze
pip freeze lists all installed packages with their versions.
Steps:
- Open a terminal.
- Run:
pip freeze > requirements.txt
3. Expected output in requirements.txt:
certifi==2021.5.30
charset-normalizer==2.0.4
flask==2.0.1
numpy==1.21.2
pandas==1.3.3
requests==2.26.0
Now, your requirements.txt file is automatically created with all installed packages.
Alternative: Using pip list
pip list also shows installed packages but does not format them like pip freeze.
Steps:
pip list > requirements.txt
Output Example (not formatted like pip freeze):
Package Version
--------------- -------
certifi 2021.5.30
charset-normalizer 2.0.4
flask 2.0.1
numpy 1.21.2
pandas 1.3.3
requests 2.26.0
Note: You may need to manually edit this output before using it in
requirements.txt.
3. Installing Dependencies from requirements.txt
Once you have a requirements.txt file, you can install all dependencies using:
pip install -r requirements.txt
Expected Output:
Collecting flask==2.0.1
Downloading Flask-2.0.1-py3-none-any.whl (94 kB)
Collecting numpy==1.21.2
Downloading numpy-1.21.2-cp39-cp39-win_amd64.whl (14.0 MB)
...
Successfully installed flask-2.0.1 numpy-1.21.2 pandas-1.3.3 requests-2.26.0
All dependencies listed in requirements.txt are now installed!
4. Best Practices for requirements.txt
(A) Use a Virtual Environment
A virtual environment isolates dependencies to prevent conflicts.
Steps:
1. Create a virtual environment:
python -m venv myenv
2. Activate the virtual environment:
- Windows:
myenv\Scripts\activate
- macOS/Linux:
source myenv/bin/activate
3. Install dependencies inside the virtual environment:
pip install flask pandas requests
(B) Use Version Pinning
Instead of listing just package names, specify versions:
flask==2.0.1
requests>=2.26.0
numpy<=1.21.2
This ensures everyone installs the same versions.
(C) Keep requirements.txt Updated
If you install new packages, update requirements.txt:
pip freeze > requirements.txt
This prevents missing dependencies.
(D) Check for Security Issues
Use pip-audit to check for vulnerabilities:
pip install pip-audit
pip-audit
This ensures secure dependencies.
5. Example Workflow (Step-by-Step Guide)
(1) Create a Virtual Environment
python -m venv myproject_env
Activate:
source myproject_env/bin/activate # macOS/Linux
myproject_env\Scripts\activate # Windows
(2) Install Required Packages
pip install flask pandas requests
(3) Generate requirements.txt
pip freeze > requirements.txt
(4) Share requirements.txt
Send requirements.txt to others.
(5) Install Dependencies on Another System
pip install -r requirements.txt
Conclusion
A requirements.txt file is essential to manage dependencies in Python projects. One might create it manually or get it automatically from pip freeze. It ensures consistency across the environments, facilitates deployment, and makes collaboration easier.