DEV Community

Cover image for TRICK: Easy requirements build
Lucas de Brito
Lucas de Brito

Posted on

TRICK: Easy requirements build

A few days ago I was doing a project in Python and wanted to let it practice for anyone who wanted to access it, and one of the steps for that was to build the requirements.txt, that commonly loads the necessary packages to run the projects in Python and the easiest alternative is:

  • view the packages in the project;
  • select the packages after executing:
$ pip freeze
Enter fullscreen mode Exit fullscreen mode

But that's not practical at all, so I looked for an alternative that would meet my need, and found a project that did just that. Its goal is generate requirements.txt based on imports in project.


To install this package, just run:

$ pip install pipreqs --user
Enter fullscreen mode Exit fullscreen mode

or, if you use Python3:

$ pip3 install pipreqs --user
Enter fullscreen mode Exit fullscreen mode

To build automatically your requirements.txt, just run the following command in the project directory:

$ pipreqs
Enter fullscreen mode Exit fullscreen mode

or

$ pipreqs /project/location
Enter fullscreen mode Exit fullscreen mode

And the magic will happen!!

Magic
I hope this post helped and feel free to get in touch! ;)
Thanks for reading!


This post is inspired by the following repository:

GitHub logo bndr / pipreqs

pipreqs - Generate pip requirements.txt file based on imports of any project. Looking for maintainers to move this project forward.

pipreqs - Generate requirements.txt file for any project based on imports

Installation

pip install pipreqs

Usage

Usage
    pipreqs [options] <path>

Options:
    --use-local           Use ONLY local package info instead of querying PyPI
    --pypi-server <url>   Use custom PyPi server
    --proxy <url>         Use Proxy, parameter will be passed to requests library. You can also just set the
                          environments parameter in your terminal:
                          $ export HTTP_PROXY="http://10.10.1.10:3128"
                          $ export HTTPS_PROXY="https://10.10.1.10:1080"
    --debug               Print debug information
    --ignore <dirs>...    Ignore extra directories
    --encoding <charset>  Use encoding parameter for file open
    --savepath <file>     Save the list of requirements in the given file
    --print               Output the list of requirements in the standard output
    --force               Overwrite existing requirements.txt
    --diff <file>         Compare modules in requirements.txt to project imports.
    --clean <file>        Clean up requirements.txt by removing modules that are not imported in project.
    --no-pin              Omit version of output packages.

Example

$ pipreqs /home/project/location
Successfully saved requirements file in /home/project/location/requirements.txt

Contents of requirements.txt

wheel==0.23.0

Latest comments (0)