Auto-generate requirements.txt file using pipreqs

At Qxf2, we love Python. We use Python on multiple projects and as per our requirements, we install multiple modules. But, I found it difficult to create a requirements.txt file for specific projects manually. So I searched for a tool/technique to generate a requirement.txt file. I come across the pipreqs package, which generates a requirement.txt file based on the import statements of the project. I like it because it is helpful in maintaining the requirements file, easy to use and saves me a lot of time.


Setup:

To get setup with the pipreqs, you need to install the pipreqs package using the following command.

pip install pipreqs

Usage:

Usage of the pipreqs is very easy. You just need to provide the root location of your project and the pipreqs automatically generate a requirements.txt file in root folder. Use the following command for usage.

pipreqs /path/to/project

The following Fig. 1 shows a snapshot of how I generated a requirements.txt file for our Qxf2’s public POM.

Fig.1 pipreqs to auto generate a requirement.txt file

The content of auto-generated requirement.txt:

requests==2.9.1
pytest==3.0.5
selenium==3.4.3
mechanize==0.2.5
python_dotenv==0.1.0
Appium_Python_Client==0.24
dotenv==0.0.5

Limitations:

Pipreqs uses imports of projects to generate a requirements.txt file. So, it is very important to note that, pipreqs will not include the plugins required for specific projects. You need to add plugins information manually in a requirement.txt.

In our case, I need to add pytest-xdist plugin details in a requirement.txt file manually.

If you are a startup finding it hard to hire technical QA engineers, learn more about Qxf2 Services.


Leave a Reply

Your email address will not be published. Required fields are marked *