13

Right now I'm using virtualenv and just switching over to Pipenv. Today in virtualenv I load in different environment variables and settings depending on whether I'm in development, production, or testingby setting DJANGO_SETTINGS_MODULE to myproject.settings.development, myproject.settings.production, and myproject.settings.testing.

I'm aware that I can set an .env file, but how can I have multiple versions of that .env file?

asked Feb 27, 2019 at 0:06

2 Answers 2

10

I'm far from a Python guru, but one solution I can think of would be to create Pipenv scripts that run shell scripts to change the PIPENV_DOTENV_LOCATION and run your startup commands.

Example Pipfile scripts:

[scripts]
development = "./scripts/development.sh"

development.sh Example:

#!/bin/sh
PIPENV_DOTENV_LOCATION=/path/to/.development_env pipenv run python test.py

Then run pipenv run development

answered Feb 27, 2019 at 1:14
Sign up to request clarification or add additional context in comments.

Comments

5

You should create different .env files with different prefixes depending on the environment, such as production.env or testing.env. With pipenv, you can use the PIPENV_DONT_LOAD_ENV=1 environment variable to prevent pipenv shell from automatically exporting the .env file and combine this with export $(cat .env | xargs).

export $(cat production.env | xargs) && PIPENV_DONT_LOAD_ENV=1 pipenv shell would configure your environment variables for production and then start a shell in the virtual environment.

answered Feb 27, 2019 at 0:15

3 Comments

There is no need to export $(cat .env | xargs): one can just specify what "version" of the environment variables file to load as specified here: pipenv.readthedocs.io/en/latest/advanced/…. In the case at hand one could do PIPENV_DOTENV_LOCATION=prod.env/test.env pipenv shell. The advantage of this approach is that your env variables aren't kept in the shell upon exiting, which otherwise would be the case in your example.
Besides the previous comment about using PIPENV_DOTENV_LOCATION, and the brokeness in the presence of characters which would cause field splitting, globbing, and so on, you can also simplify this method to just: $(cat .env | xargs) PIPENV_DONT_LOAD_ENV=1 pipenv shell, and thereby avoid leaking exported variables into all commands after this one.
Actually I got so focused on my prior points that I totally forgot that I think you can simplify this further: you don't need | xargs at all, because in Bourne-like shells newlines in substituted output will work just like spaces - they cause field splitting (unless you've messed with your shell's field splitting, such as by setting the IFS variable), but they will never be treated as statement separators.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.