Date Tags Python

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

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 pipreqs.

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\


Note that 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:


comments powered by Disqus