How do you use python-decouple to load a .env file outside the expected paths?
Asked Answered
S

3

32

I'm forced to keep my .env file in a non-standard path outside the root of my project (in a separate directory altogether).

Let's say I have my Django project in /var/projects/my_project, though I have my .env file in /opt/envs/my-project/.env where my SECRET_KEY is stored. In my settings.py file, I'd like to explicitly use the .env file at that path so that I can still do this:

from decouple import config
secret_key = config('SECRET_KEY')
Samuelson answered 23/4, 2017 at 11:56 Comment(1)
Keeping the .env file outside the project root folder is a good methodology in terms of security wise!Allsun
S
56

I figured it out.

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

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')

Hopefully this helps someone.

Samuelson answered 23/4, 2017 at 16:38 Comment(2)
Which version did you use? django-decouple version 2.1 doesn't seem to have Config or RepositoryEnv.Sweeting
@BarneySzabolcs I'm using 3.8 versionConcordance
L
25

If you look at the decouple implementation, config is just a pre-instantiated AutoConfig:

config = AutoConfig()

But AutoConfig takes as optional argument search_path so we can do the following:

from decouple import AutoConfig
config = AutoConfig(search_path='/opt/envs/my-project')

Then you can do as usual:

secret_key = config('SECRET_KEY')
Lucubration answered 19/6, 2020 at 19:26 Comment(3)
I was testing with a different folder within the root of the git repo and had to do this.Saraann
Which version did you use? django-decouple version 2.1 doesn't seem to support arguments for AutoConfig's constructor.Sweeting
I use 3.6 version and it works wellDenicedenie
S
2

Now, django-decouple==2.1 supports having settings.ini and .env files in any parent directory of the project dir.

(And the old methods don't work anymore. - from decouple import Config, RepositoryEnv does not work, AutoConfig does not have search_path as parameter.)

This is convenient because you would want to keep the settings.ini in the project folder on your local machine and you would want to have clean checkouts on the staging/prod server, thus the settings.ini is better located outside the project folder.

UPDATE:

I would still explicitly define the location of the settings file.
(I'm currently using python-decouple==3.8)

from decouple import Config, RepositoryEnv
from os.path import join
from mysite import BASE_DIR
config = Config(RepositoryEnv(join(BASE_DIR, 'settings.ini')))

DEBUG = config('DEBUG', cast=bool)
secret_key = config('SECRET_KEY')
Sweeting answered 16/12, 2021 at 0:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.