DEV Community

Cover image for Automate the task of creating requirements.txt with pyreqs!
Arnav Raina
Arnav Raina

Posted on

Automate the task of creating requirements.txt with pyreqs!

Problem

All us python programmers have been in a position when our Python project is just ready and its time to share or ship it and we need to create our python dependencies (requirements.txt). And hence comes the time-consuming procedure of looking at our imports and adding them to our requirements.txt 😮‍💨.

Solution

We can easily go for pip freeze > requirements.txt that adds all the packages in our current python environment to the file. But there are some downsides to that:

  • pip freeze only saves the packages that are installed with pip install in our environment; and
  • it saves all packages in the environment including those that we don't use in our current project.

During my journey of learning go, I came to realize its beauty in the creation of CLI applications giving me a great project idea to work on the above mentioned issue.

pyreqs

So I created pyreqs. It is a command-line application written in go for automating the task of creating requirements.txt for any python project.

main-cmd-win

Just get a compatible release for your system, and go to your python project. To create requirements.txt, just run the command:

pyreqs create
Enter fullscreen mode Exit fullscreen mode

pyreqs analyzes all the imports that have been imported in our .py files and is intelligent enough to ignore directory imports and only add the relevant ones to our requirements.txt. We can also specify whether the imports versioning need to be taken from our virtual environment (venv) directory or not by passing in the -v flag along with the venv directory path.

We can get a detailed info about the imports, by running with --debug flag:

pyreqs create --debug
Enter fullscreen mode Exit fullscreen mode

pyreqs-with-debug

The create command is available with many other flags:

create-cmd

Also, sometimes we have the requirements.txt file and we just want to clean it up according to our project imports, pyreqs does that with the clean command:

pyreqs clean -r /path/to/requirements.txt
Enter fullscreen mode Exit fullscreen mode

There are various optional flags that can be used with clean as seen below:
clean-cmd

I hope this tool makes the development process a little smooth for the fellow python developers out there! Peace ✌️.

Top comments (9)

Collapse
 
cwprogram profile image
Chris White

I would say that for modern python development most projects want to transition to a pyproject.toml based solution instead of requirements.txt. Poetry and PDM are some popular interfaces to this.

Another nice feature of pyproject.toml is the ability to group your dependencies. This means you could have a category of documentation dependencies (ala sphinx), linting dependencies, and test suite dependencies. It's particularly useful for lean production deployments where you want only dependencies needed for running your application present.

Collapse
 
arnavrneo profile image
Arnav Raina

Yes you are absolutely right! Poetry is suitable for complex and production ready projects. But for everyday use-cases, pip is still the most basic and simple option. And so, requirements.txt becomes necessary!

Collapse
 
nitkarshchourasia profile image
Nitkarsh Chourasia (Pragmatic_Developer)

What about pipreqs? and pipenvs?
How to install yours, on Windows machine?
(Mine windows 10)
Thank you.

Collapse
 
arnavrneo profile image
Arnav Raina • Edited

Yes, pipreqs is also good. It's just that it's developed in python while pyreqs is developed in go. And as go is faster compared to python, there can be significant speed improvements.

I have specified the installation steps on the github!

Your welcome!

Collapse
 
nitkarshchourasia profile image
Nitkarsh Chourasia (Pragmatic_Developer)

Now, it's better. Thanks for updating the repository.

Collapse
 
nitkarshchourasia profile image
Nitkarsh Chourasia (Pragmatic_Developer)

Is it available in Anaconda?

Collapse
 
arnavrneo profile image
Arnav Raina • Edited

It's developed in golang, so not available on Anaconda. You have to download it as a binary.

Collapse
 
benbb96 profile image
BBB

Interesting, I'll try to use it next project.

Collapse
 
arnavrneo profile image
Arnav Raina

Thanks! 😄