4

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.

capek
  • 241
  • 1
  • 7
  • The main point off using a `requirements.txt` is to make use of it within a `virtualenv`, and that file has all the dependencies, are you wanting to know how to use it outside of the `virtualenv` or `venv`? See this [link](https://medium.com/@boscacci/why-and-how-to-make-a-requirements-txt-f329c685181e). – de_classified Sep 10 '20 at 19:12
  • @FishingCode I want to know how to manage two venv's (and the resulting requirements.txt which is created with pip freeze), one for production and one for development which has additional packages. – capek Sep 10 '20 at 19:17
  • What you coud do/look into is that, create two requirements.txt one for production and the other for development (which has more packages) and create some logic to determine which `virtualenv` should run which requirements.txt. You could do that with a `.bat` file, and if you require different python versions you could add that as well. – de_classified Sep 10 '20 at 19:31

1 Answers1

5

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
Dustin Ingram
  • 20,502
  • 7
  • 59
  • 82