How to customize a requirements.txt for multiple environments? How to customize a requirements.txt for multiple environments? python python

How to customize a requirements.txt for multiple environments?


You can cascade your requirements files and use the "-r" flag to tell pip to include the contents of one file inside another. You can break out your requirements into a modular folder hierarchy like this:

`-- django_project_root|-- requirements|   |-- common.txt|   |-- dev.txt|   `-- prod.txt`-- requirements.txt

The files' contents would look like this:

common.txt:

# Contains requirements common to all environmentsreq1==1.0req2==1.0req3==1.0...

dev.txt:

# Specifies only dev-specific requirements# But imports the common ones too-r common.txtdev_req==1.0...

prod.txt:

# Same for prod...-r common.txtprod_req==1.0...

Outside of Heroku, you can now setup environments like this:

pip install -r requirements/dev.txt

or

pip install -r requirements/prod.txt

Since Heroku looks specifically for "requirements.txt" at the project root, it should just mirror prod, like this:

requirements.txt:

# Mirrors prod-r requirements/prod.txt


A viable option today which didn't exist when the original question and answer was posted is to use pipenv instead of pip to manage dependencies.

With pipenv, manually managing two separate requirement files like with pip is no longer necessary, and instead pipenv manages the development and production packages itself via interactions on the command line.

To install a package for use in both production and development:

pipenv install <package>

To install a package for the development environment only:

pipenv install <package> --dev

Via those commands, pipenv stores and manages the environment configuration in two files (Pipfile and Pipfile.lock). Heroku's current Python buildpack natively supports pipenv and will configure itself from Pipfile.lock if it exists instead of requirements.txt.

See the pipenv link for full documentation of the tool.


If your requirement is to be able to switch between environments on the same machine, it may be necessary to create different virtualenv folders for each environment you need to switch to.

python3 -m venv venv_devsource venv_dev/bin/activatepip install -r pip/common.txtpip install -r pip/dev.txtexitpython3 -m venv venv_prodsource venv_prod/bin/activatepip install -r pip/common.txtexitsource venv_dev/bin/activate# now we are in dev environment so your code editor and build systems will work.# let's install a new dev package:# pip install awesome# pip freeze -r pip/temp.txt# find that package, put it into pip/dev.txt# rm pip/temp.txt# pretty cumbersome, but it works.