How to Create Requirements.txt File in Python
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:
- Reproducibility: By specifying the exact versions of packages and libraries your project depends on, you ensure that anyone else working on your project can create the same environment and reproduce the same results.
- Dependency Management: Managing dependencies becomes more manageable. You can easily add, update or remove packages as needed without keeping track of them manually.
- Compatibility: It helps avoid version conflicts between packages, ensuring that all dependencies work together harmoniously.
- Deployment: When deploying your application or sharing it with others, you can provide them with a requirements.txt file to install the necessary dependencies effortlessly.
Steps to Create a requirements.txt File
Step 1: Set up a virtual environment
Using a virtual environment isolates your project’s dependencies, preventing conflicts with system-wide Python packages.
- Open a terminal and navigate to your project directory.
- Create and activate a virtual environment using the following commands.
# 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
Step 2: Install Dependencies
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:
pip install pandas
Step 3: Generate the requirements.txt File
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 file
After generating the file, review its contents using :
cat requirements.txt
It should list installed packages with their versions, like:
pandas==1.5.3
numpy==1.23.5
Step 5: Install dependencies from requirement.txt
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.txt
- Regularly Update Dependencies: Periodically update the file by running pip freeze > requirements.txt after adding new packages.
- Pin Package Versions: Specify exact versions to avoid compatibility issues (e.g., numpy==1.23.5 instead of numpy).
- Use a Separate Dev Requirements File: Maintain a dev-requirements.txt for development dependencies like testing tools.
- Use Virtual Environments: Always create and use a virtual environment to manage dependencies efficiently.
Similar Reads
How to Create Requirements in TestLink? Creating requirements in TestLink is a crucial process for test management and quality assurance. TestLink is a popular open-source test management tool that helps teams define, track, and manage requirements effectively. By establishing clear requirements, you ensure that your testing efforts are a
3 min read
How to remove blank lines from a .txt file in Python Many times we face the problem where we need to remove blank lines or empty spaces in lines between our text to make it look more structured, concise, and organized. This article is going to cover two different methods to remove those blank lines from a .txt file using Python code. This is going to
3 min read
What Are the Essential Software Requirements for Python Programming? Python has become one of the most popular programming languages due to its simplicity, readability, and versatility. It is a great choice for data science, machine learning, web development, or automation. However, before writing your first Python script, itâs crucial to ensure your system is set up
5 min read
Reading and Writing to text files in Python Python provides built-in functions for creating, writing, and reading files. Two types of files can be handled in Python, normal text files and binary files (written in binary language, 0s, and 1s). Text files: In this type of file, Each line of text is terminated with a special character called EOL
8 min read
How To Embed Python Code In Batch Script Embedding Python code in a batch script can be very helpful for automating tasks that require both shell commands and Python scripting. In this article, we will discuss how to embed Python code within a batch script. What are Batch Scripts and Python Code?Batch scripts are text files containing a se
4 min read