Python - Auto generate requirements.txt

Why requirements.txt?

Any application typically has a set of dependencies that are required for that application to work. The requirements file is a way to specify and install specific set of package dependencies at once.

Format for requirements file:

requests==1.2.0
Flask==0.10.1

Method 1:

$ pip freeze > requirements.txt

Use pip’s freeze command to generate a requirements.txt file for your project: If you save this in requirements.txt, then you can pip install -r requirements.txt.

Method 2:

Use pipreqs - pipreqs used to generate requirements.txt file for any project based on imports

Why not pip freeze ?

  • pip freeze saves all packages in the environment including those that you don’t use in your current project. (if you don’t have virtualenv)
  • pip freeze only saves the packages that are installed with pip install in your environment.
  • sometimes you just need to create requirements.txt for a new project without installing modules.

How to use?

$ pip install pipreqs

$ pipreqs /path/to/project

for additional options see https://github.com/bndr/pipreqs