HBNetwork / python-decouple

Strict separation of config from code.
MIT License
2.83k stars 196 forks source link

How would you use a .env file that is in a different directory #105

Closed nh916 closed 3 years ago

nh916 commented 3 years ago

I have a .env file that is in a different directory than the root directory and I noticed that python-decouple does not find it and only finds .env file if it is within the root directory. I was wondering how I can specify the path to my .env file

nh916 commented 3 years ago

from decouple import Config, RepositoryEnv

DOTENV_FILE = '/opt/envs/my-project/.env'
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')