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.
Just get a compatible release for your system, and go to your python project. To create requirements.txt
, just run the command:
pyreqs create
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
The create
command is available with many other flags:
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
There are various optional flags that can be used with clean
as seen below:
I hope this tool makes the development process a little smooth for the fellow python developers out there! Peace โ๏ธ.
Top comments (9)
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.
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!What about pipreqs? and pipenvs?
How to install yours, on Windows machine?
(Mine windows 10)
Thank you.
Yes,
pipreqs
is also good. It's just that it's developed in python whilepyreqs
is developed ingo
. And asgo
is faster compared topython
, there can be significant speed improvements.I have specified the installation steps on the github!
Your welcome!
Now, it's better. Thanks for updating the repository.
Is it available in Anaconda?
It's developed in golang, so not available on Anaconda. You have to download it as a binary.
Interesting, I'll try to use it next project.
Thanks! ๐