Pipreqs Tutorial – How to Create Requirements.txt File In Python with Ease

Reproducibility and re-usability of your Python projects is an essential aspect of being a software developer or engineer, and one of the ways to do so is to provide means that others can get the same working environments you used for your projects and the same versions of packages and libraries used.

Among the many ways we achieve that is using a virtual environment and docker. But what if you only want to get the packages and libraries used in a project and give it to someone without setting up a virtual environment. What if you only want the packages imported and used in your python scripts and not the entire environment or system?

Introducing pipreqs – a simple library to automatically generate and create requirements.txt file for your Python projects and scripts – whether you are working in a virtual environment or not.

Let us see how to work with it

Installing Pipreqs

Pipreqs can be installed using pip since it is already available on PYPI.

pip install pipreqs

or using Pip3 (For python3)

pip3 install pipreqs

Working with Pipreqs

Pipreqs is quite simple and straight to the point – all you have to do is to point it to the project or folder with your python script/file. Simply do

pipreqs path/to/python_project

This will automatically scan the folder and identify the .py file and extract all the various imported libraries and packages and do some magic and generate a requirements.txt with their pinned versions.

In case you want to preview the packages without writing to the requirements.txt file you can also do so using

pipreqs --print path/to/python_project
Omitting Pinned Versions

To get the packages without their pinned versions

pipreqs --no-pin path/to/python_project
Save Imported Packages to A Different File

You can also save the imported packages to a path or a file of your choice using the –savepath option

pipreqs –savepath packages.txt path/to/python_project

In this case it will save the imported packages to the packages.txt file after automatically creating that file.

Ignoring Specific Folders/Directory

So what if you have multiple directories in your project and you don’t want certain directories’ imports?. You can use

pipreqs --ignore project01/extra_app project01

There are other things you can also do such as cleaning imports, comparing difference in requirements.txt ,etc.

You can see how pipreqs make thing quite easier when creating requirements.txt for your projects.

You can also check out the video tutorial below

Thank You For Your Time

Jesus Saves

By Jesse E. Agbe(JCharis)

Leave a Comment

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