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.
requirements.txt fileTo 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.txtfile will also include the version number of each module, package, library, and framework. It serves as a list of items to be installed bypip.
requirements.txt fileOnce 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.
For example, given a requirements.txt file:
Flask==2.3.2SQLAlchemy==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
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