How to make a requirements.txt file in Python

To make a requirements.txt file in Python,

  1. Go to a project directory. 
  2. Open a new cmd or terminal and navigate it to the project directory.
  3. After locating the project directory, type this command: pip freeze > requirements.txt. This command is used to find all the libraries used, and then it writes all the libraries used in the requirements.txt file. Now, hit Enter.

Check the project directory, and you can see that a new requirements.txt file has been created. If you open this file there, you can see all the libraries used in the project. At the same time, you are sharing the project. We have to share this requirements.txt file and the project folder to make it easy for the team member to view the project.

What is the requirements.txt file in Python?

Python requirement.txt file consisting of all the packages, modules, and libraries used while creating the project. This file is essential because if a new user downloads the code from the online platforms and runs it on their computer.

The program will not execute because the user would not have all the libraries used in the project for easy access to the user. This text file is important for accomplishing any project.

During the creation of the project, we will import lots of libraries and use many modules, but we don’t keep track of what libraries have been used, so it will be difficult in a workplace where we alone will not be creating the project.

There may be many team members, so if they try to run this project, they will face errors like the package is not installed.

So this may cause several issues to overcome these types of problems. Using this file, any new member can easily install all the packages used in the project.

Making requirements.txt file using pipreqs

To use the pipreqs package in Python, install it.

python3 -m pip install pipreqs

And after installing, type the following command.

pipreqs path

Here the path is the location where the project folder is present. For example, let us consider there is a project named suduko_solver on the desktop. See the below command:

pipreqs \users\python\desktop\suduko_solver

This command will create a requirements.txt file in the project location.

Now we have seen how to create the requirements.txt now.

Let’s see how to use the requirements.txt file to install all the packages used in the project. This is also important because we don’t need to install all the packages described in requirements.txt. Manually this command can be used to automatically install all the dependent packages.

Open a new cmd or terminal, go to the project file location, and type the following content.

python3 -m pip install –r requirements.txt

This command will automatically install all the dependent libraries into your system. One more thing to note is that this requirements.txt file name can be changed.

There is no rule that this file should contain the requirements.txt as a file name. You can use dependencies.txt or anything other than that. However, this file is most widely named requirements.txt for standard development practice.

That’s it for this tutorial.

See also

How to write json file in Python

How to list files in a directory

    Leave a Comment