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

后端 未结 2 1352
太阳男子
太阳男子 2021-02-14 05:47

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 pro

2条回答
  •  梦谈多话
    2021-02-14 06:23

    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.

提交回复
热议问题