1

I am working on a django project that I need to run it with Docker. In this project I have multiples .env files: .env.dev, .env.prod, .env.staging. Is there a right way to manage all this file with the package python-decouple? I've search for a workaround to deal with this challenge and do not find any kind of answer, not even on the official documentation.

Can I use something like:

# dont works that way, it's just a dummie example
python manage.py runserver --env-file=.env.prod

or maybe any way to setting or override the file I need to use?

1 Answers1

1

Instead of importing decouple.config and doing the usual config('SOME_ENV_VAR'), create a new decouple.Config object using RepositoryEnv('/path/to/.env.prod').

from decouple import Config, RepositoryEnv

DOTENV_FILE = '/home/user/my-project/.env.prod'
env_config = Config(RepositoryEnv(DOTENV_FILE))

# use the Config().get() method as you normally would since 
# decouple.config uses that internally. 
# i.e. config('SECRET_KEY') = env_config.get('SECRET_KEY')
SECRET_KEY = env_config.get('SECRET_KEY')
Boro
  • 118
  • 6
  • 1
    Calling an absolute path like `'/home/user/my-project/.env.prod'` is not a good thing. Does this work with relative paths? – Ramon Dias Aug 05 '22 at 13:44