How to generate requirements.txt automatically for your python project using Pigar
As a Python Developer working on shared codebases, its always beneficial to generate a requirements.txt file for your projects, so that the dependencies can be managed better.
How do we do this without spending too much time?
An easier way is to generate the requirements.txt is by generating the file from pip itself like this:
For Python2.x:
pip freeze > requirements.txt
For Python3.x:
pip3 freeze > requirements.txt
But is there an even easier way to do it and list the dependencies of each python file? Pigar to the rescue.
We can install Pigar by running:
pip install pigar
In the directory containing your python script, just run the following in terminal:
pigar
This should automatically create a requirements.txt like this with reference for each of the python files in the directory:
# Automatically generated by https://github.com/damnever/pigar.
# pigar/helpers.py: 12
colorama == 0.4.3
# pigar/parser.py: 16
nbformat == 5.0.4
# pigar/core.py: 28
# pigar/pypi.py: 19
requests == 2.23.0
Voila! your requirements.txt is ready and can be installed as :
pip install -r requirements.txt
Hope you enjoyed the short read.
References: