How to use the requirements.txt file in Python

In this Answer, we’ll learn to use the requirements.txt file in Python using the PIP. The requirements.txt file is extremely helpful when sharing code via source code management tools like Git, GitHub, GitLab, and Bitbucket. It keeps track of modules, packages, libraries, and frameworks required for the projects. In other words, it contains all of the project requirements.

Create a requirements.txt file

To create the requirements.txt file, we can use the following command:

pip freeze > requirements.txt

A new file will be created in the directory when we run this command. This will list the modules, packages, libraries, and frameworks required for the project.

Note: The requirements.txt file will also include the version number of each module, package, library, and framework. It serves as a list of items to be installed by pip.

Use a requirements.txt file

Once a requirements.txt file is present in the project directory, we can run the following command to install the project requirements.

pip install -r requirements.txt

When we run this command, the terminal will list all the modules, packages, libraries, and frameworks installed.

Example

For example, given a requirements.txt file:

Flask==2.3.2
SQLAlchemy==2.0.19

Running the pip install -r requirements.txt command will install the project requirements with specified versions as follows:

Flask==2.3.2
SQLAlchemy==2.0.19
Installing the project requirements

Note: Most of the time, the requirements.txt file is used to set up a virtual environment. We suggest reading the following Answers to get familiarized with the concepts:

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved