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

后端 未结 2 1398
太阳男子
太阳男子 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:10

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

提交回复
热议问题