The Python requirements file is a simple text file containing all the modules and packages (along with their specific versions) the project requires. This file ensures the smooth working of projects. The requirements file is created as a simple file with a “.txt” extension inside the project folder which makes it easy for programmers to install them.
This write-up will demonstrate how to create a Python requirements file.
How To Create a Python Requirements File Using Pip Freeze?
To create a Python requirements file, open your IDE or code editor, click on the command line terminal, and run the below-listed command:
$ pip freeze > requirements.txt
It can be seen that after executing the above command, a new requirements.txt file is created
Now we can install the required packages using the following command:
pip install -r requirements.txt
We can also update packages. First, let’s check the outdated packages using the command
$ pip list --outdated
Let’s update Streamlit using the command shown below:
$ pip install -U streamlit
It will take some time to update the desired package:
The package has been updated successfully as shown below:
How to Create a Python Requirements File Using pipreqs?
To create a Python requirements file using pipreqs, first install pipreq using the below-listed command:
$ pip install pipreqs
Now to create a requirements file, execute the following command:
$ pipreqs Path\to\project
Path to Project simply means you have to provide the complete path (location) of your project on your PC.
In our scenario, the path is C:\Users\USER\OneDrive\Desktop\Python
It can be seen that a new requirements file is created successfully.
Now we can install a bunch of packages as well as libraries using the requirements.txt file as we did in the previous method.
Let’s install matplotlib using the requirements.txt file:
After a few minutes, matplotlib will be installed successfully.
That’s all about creating a requirement file in Python.
Conclusion
Python requirements file is a simple text file containing all the modules and packages the project requires. Among the two methods, in the first method, open a code editor and click on the Command line terminal. Execute the “pip freeze > requirements.txt” command. In the second method, install prereqs using the “pip install prereqs” command and use the “pip install path\to\project” command to create a requirements file. This guide has illustrated how to create a Python requirements file using different methods.