Creating a requirements.txt without development dependencies

Viewed 412

In development often additional packages are needed which I have installed in my venv, which are not needed in production contrary to other packages I also installed in my venv. What's the cleanest way to produce a requirements.txt which does have all the requirements except the once I only need in development (like mypy or autopep8)

This answer shows how to do it but the method is very cumbersome.

1 Answers

I'd recommend using pip-compile, from https://pypi.org/project/pip-tools/.

This lets you define an "in" file, requirements.in, which just lists your top-level dependencies, e.g.:

requirements.in:

flask

Then you generate the requirements.txt with pip-compile:

$ pip-compile --output-file=requirements.txt requirements.in
#
# This file is autogenerated by pip-compile
# To update, run:
#
#    pip-compile --output-file=requirements.txt requirements.in
#
click==7.1.2              # via flask
flask==1.1.2              # via -r requirements.in
itsdangerous==1.1.0       # via flask
jinja2==2.11.2            # via flask
markupsafe==1.1.1         # via jinja2
werkzeug==1.0.1           # via flask

Then you can have separate .in/.txt files for different environments, like test.in/test.txt.

And finally, you can recursively include multiple requirements files, e.g.

dev-requirements.txt:

-r requirements.txt
-r test.txt
-r lint.txt
Related