Generating Python requirements files
I am always forgetting how to do this so I thought I would throw together a quick post.
You can generate a requirements file manually for a script by following the proper requirements.txt format and simply tpying them out. But that is slow, prone to error, and super boring! :)
There are two ways I know of though to make the process a lot easier.
Generating a requirements file for an entire Python environment
$ pip freeze > requirements.txt $ cat requirements.txt backports-abc==0.4 backports.ssl-match-hostname==18.104.22.168 certifi==2016.2.28 configparser==3.3.0.post2 decorator==4.0.9 entrypoints==0.2 functools32==3.2.3.post2
This command will read your python environment and store all the modules you have installed with pip into the proper requirements.txt format. This might be helpful if you want to create a new virtual environment with all the packages of another environment.
You could then do something like:
$ pip install -r requirements.txt
Generating a requirements file for a specific Python script
A lot of times though, what you want to do it actually create a requirements.txt for a git repository and you only want to list the packages required to run that script; packages that the script imports.
To do that you can use a module called
To use it you will first need to install it:
$ pip install pipreqs
Now you can generate the requirements file for a script
$ pipreqs path\to\myPyFile.py
pipreqs doesn't work currently on Windows environments using virtual environments. The developer of
pipreqs doesn't have a Windows machine so he can’t easily debug the issue: https://github.com/bndr/pipreqs/issues/23