How to automatically generate requirements.txt 📃 for your Python project.
You’ve worked hard to complete your project and you’re finally ready to publish it either Opensource it on Github, or create a Docker image or maybe a personal project requirement, generating a requirements.txt with the right versions for your libraries seems rather exhaustive.
This becomes essential especially if you have a large number of dependencies and libraries installed for a particular project some of which might only work on specific library versions.
There are 2 easy ways of doing this:
Method 1: pip environment
If you work with virtual environments like pip this is the easiest way to generate requirements file.
First you will need to have pip environment setup in your machine.
Then just execute this simple command
pip3 freeze > requirements.txt
Method 2: For individual projects
If you don’t have a virtual environment use pigar https://github.com/Damnever/pigar
It’s a wonderful library that’ll make your life easy 😉
Install pigar with this command
$ sudo pip install pigar
Then in the directory containing your .py python files execute this in the terminal
# Generate requirements.txt for current directory.
$ pigar
# Generating requirements.txt for given directory in given file.
$ pigar -p ../dev-requirements.txt -P ../
As simple as it can be, Enjoy 🎉