Last Updated : 13 Mar, 2025
Creating and maintaining a requirements.txt file is a fundamental best practice for Python development. It ensures that your project's dependencies are well-documented and easily reproducible, making it easier for others to work on your code and reducing the likelihood of compatibility issues.
Why Use a requirements.txt File?A requirements.txt file serves several important purposes:
Using a virtual environment isolates your project’s dependencies, preventing conflicts with system-wide Python packages.
Step 2: Install Dependencies# Create a virtual environment
python -m venv myenv# Activate the virtual environment
# on Windows
myenv\Scripts\activate# on macOS and Linux
source myenv/bin/activate
Install the necessary dependencies for your project using pip. Replace package-name with the actual package you want to install.
pip install package-name
For example, to install pandas:
Step 3: Generate the requirements.txt Filepip install pandas
Once all required packages are installed, generate the requirements.txt file by running:
pip freeze > requirements.txt
This command captures all installed dependencies and their versions in the file.
Step 4: Review your requirement.txt fileAfter generating the file, review its contents using :
cat requirements.txt
It should list installed packages with their versions, like:
Step 5: Install dependencies from requirement.txtpandas==1.5.3
numpy==1.23.5
When setting up the project on a new system, install all dependencies using:
pip install -r requirements.txt
This ensures that all required packages are installed with the specified versions.
Best Practices for Managing requirements.txtRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4