Python Requirements.txt: What it is and How to Use it?

requirements.txt

Hello readers! In this article, we will be delving into the topic of requirements.txt, a file that plays a crucial role in ensuring the smooth functioning of code projects. If you are new to coding or have experience working with coding projects, you may have faced the issue of running the code and finding out that it doesn’t work correctly due to missing dependencies/packages. This can be a frustrating and time-consuming process, especially in projects with a large number of dependencies.

What are dependencies?

For those who are unfamiliar, dependencies refer to the libraries or packages that a code project relies on to run correctly. These dependencies must be installed before the code can function properly, and when they are missing, the code won’t run as expected. The process of finding and installing missing packages can be a hassle, especially for projects with a large number of dependencies.

Let’s take a very simple example

from numpy import linalg

vector = [-8, 6, 0, -15]
#calculate l1 norm
l2 = linalg.norm(vector)
print("L2 norm =",l2)

The code above uses the numpy library in Python, specifically importing the function linalg.norm to print the norm of a vector (array). The numpy library is a dependency here because, without it, it would not be possible to calculate the norm of the given vector.

What is requirements.txt?

The requirements.txt file lists all the dependencies that a project requires, along with the specific version of each dependency. This file is located directly inside the project folder, making it easy for other developers to access and install the necessary dependencies.

By creating a requirements.txt file, developers can save time and ensure that the code will run correctly without any issues. The file looks similar to the one below

Requirements Txt
Sample requirements.txt

In the file above, each line represents a dependency that is necessary for the project to run. The line begins with the packages’ name, followed by “==”, which serves as a separator, signaling that the following half of the line will include the precise version number necessary for the project.

An example of this format is shown in the picture, where the project requires two specific modules for Python: the tinyhtml module, with a required version of 1.2.0, and the numpy module, with a required version of 1.21.6.

Generate requirements.txt automatically for a project

To automatically produce requirements.txt, first install the pipreqs package via pip.

pip install pipreqs

Check the package version to see if pipreqs is correctly installed.

pipreqs --version

Now copy the path of the folder of the project whose dependencies you want to generate in requirements.txt and write the following command in the terminal

pipreqs path\to\project

This will create a requirements.txt file in your project folder and print a success message. For example:

Screenshot 20230213 013812
Windows terminal example

If you haven’t installed pip in your system then visit: Pip installation

Installing packages using requirements.txt

To install the listed packages you can follow the following steps

  • Launch your terminal or command prompt.
  • If you are using a virtual environment, then activate it, and then perform the following instructions to install the packages in the virtual environment.
  • Navigate to the directory where your requirements.txt the file is located.
  • Run the following command
pip install -r requirements.txt
  • If you are not in the directory where the requirements.txt is located, you can simply run
pip install -r path/to/requirements.txt
  • After the installation is complete, execute the following command to see if all of the packages have been installed:
pip freeze
  • This command will list all of the packages and versions that are presently installed in your environment.

Also read: Pipenv: The New Packaging Tool For Python

Conclusion

In this article, we learned that the requirements.txt file is a crucial aspect of code projects that helps ensure that dependencies are properly installed, and the code runs correctly. We also discussed how to create and run the file automatically, both for your own projects and for projects that you have no prior knowledge of.

References

https://pip.pypa.io/en/stable/reference/requirements-file-format/