Storing Config in Environment Python / Django

2019-05-19 11:51发布

问题:

I have been wondering about this for awhile. Namely, how does one go about isolating the config from the codebase when doing projects in Python and/or Django.

In particular, these thoughts have been inspired by one of Heroku's 12 Factors: III Config

What are some ways one could do this? How could he comply with this requirement when writing projects in Python, especially Django?

回答1:

Using Environment Variables

The most common way to solve this problem is to use environment variables.

For example in a Django app's settings.py you could write the following:

import os
if os.getenv("DJANGO_DEBUG") == '1':
    DEBUG = True

API keys would also be very similar:

import os
API_KEY = os.getenv("API_KEY", None)

Another way of doing this using the django-environ package was suggested by FlipperPA. It was inspired by 12factor, so it seems like a perfect match for this question.

If you deploy using Heroku then you can write your own environment variables for each dyno under settings. If you use another deployment method like AWS CodeDeploy you can write a shell script to set the environment variables by retrieving them from a secure store (like AWS Paramater Store)

Setting Environment Variables

To set these environment variables depends on your Operating System. Environment variables are also inherited from your command line, so for Linux you would have to add the environment variable to your run scripts and on Windows you can add a system variable.

Windows system environment variable (Universally accessible environment variable):

setx API_KEY abcdefghijklmnopqrstuvwxyz123

Windows temporary environment variable (All processes invoked after running this command can access the variable):

set API_KEY abcdefghijklmnopqrstuvwxyz123

Adding a environment variable for all processes in Linux is more complicated, follow this answer to make an environment variable automatically set when a shell starts.

Linux temporary environment variable (All processes invoked after running this command can access the variable):

export API_KEY="abcdefghijklmnopqrstuvwxyz123"

Testing Environment Variables

To test the environment variables set you can use the following command (Replace "API_KEY" with whatever the name of the variable is):

Windows:

echo %API_KEY%

Linux:

echo $API_KEY

If theses return the same thing you entered it should be working. You can try it out in python by typing:

python

You should then see something like the following:

Python 3.6.2 (default, Jul 17 2017, 16:44:45)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

You can then write your python to test it out:

import os
os.getenv("API_KEY")

And you should see the value of your environment variable printed out.