Make requirements.txt in python – Java2Blog

[ad_1]

What is the requirements.txt file in Python?

Every package is also updated regularly and features get added and deprecated with time. Every Python script may use any desired number of packages for different functionalities and objects.

The requirements.txt is a simple text file generally added by the developers that contain a list of all the packages and their specific versions used in a given Python project. This is very common, especially among GitHub projects, which allows anyone who wishes to use this project to install the required packages first for it to work.

Generally, people create virtual environments for each project. A virtual environment creates a separate environment with a specific interpreter and packages isolated from the other environments. This is very useful for only installing the required packages for a specific project. With the requirements.txt file, anyone can download the packages to a given environment with a single line of command.

The command is shown below to install packages listed on the requirements.txt file.

Now that we have discussed the requirements.txt file in Python, let us see how to create such a file.

Ways to make requirements.txt file in Python

In this article, we will learn how to make requirements.txt file in Python

Using the pip to make requirements.txt in Python

Python provides a package manager called pip that can be used to install and uninstall different packages. We can use the pip command via the command line as well.

The freeze command with pip lists the package installed via pip. To create the requirements.txt file, we can export this to a text file.

See the command below.

We need to go to the directory of the project and execute the above command based on the version of Python installed. The file will be created in the same directory.

Using the conda command to make requirements.txt file in Python

Anaconda is an IDE vastly used by data scientists for working with Python and R programming languages. This IDE uses conda prompt to install and download packages for the IDE.

For users working with Anaconda, we can use the conda command to make requirements.txt in Python. The list command with conda prompt returns the list of the packages, and this can be exported to the requirements.txt file.

Use the following command.

Using the pipreqs package to make requirements.txt file in Python

The above two methods are most frequently used to make requirements.txt in Python. However, their downside is that they will save information about all the packages in the environment, even those which are not used in the project.

This is where pipreqs become useful. We can use the pip manager to install this package and then use it within the directory of the project to create the requirements.txt file.

See the commands below.

Conclusion

To conclude, we discussed different ways to make requirements.txt file in Python. The first section gave a brief overview of the file and how to install packages listed on the file. Then the methods were discussed to make requirements.txt file in Python. The Python package manager pip can be used to create such a file. Users working with Anaconda can also use the conda package to make requirements.txt file in Python. The final method involved the use of the pipreqs module that can also create the file, but this module needs to be installed first.

[ad_2]

Leave a Comment