在环境Python / Django中存储配置 [英] Storing Config in Environment Python / Django

查看:88
本文介绍了在环境Python / Django中存储配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在想这个。即,在Python和/或Django中进行项目时,如何将配置与代码库隔离。

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.

特别是,这些想法是受到Heroku的12个因素之一启发的: III配置

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

有哪些方法可以做到这一点?在用Python(尤其是Django)编写项目时,他如何满足这一要求?

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

推荐答案

使用环境变量



解决此问题的最常见方法是使用环境变量。

Using Environment Variables

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

例如,在Django应用的settings.py中,您可以编写以下内容:

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密钥也非常相似:

API keys would also be very similar:

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

使用 django-environ 软件包 FlipperPA 。它的灵感来自12factor,因此似乎很适合这个问题。

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.

如果使用Heroku进行部署,则可以在设置下为每个dyno编写自己的环境变量。如果您使用其他部署方法(例如AWS CodeDeploy),则可以编写Shell脚本以通过从安全存储中检索环境变量来设置环境变量(例如AWS 参数店

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)

要设置这些环境变量取决于您的操作系统。环境变量也从命令行继承,因此对于Linux,您必须将环境变量添加到运行脚本中,而在Windows上,您可以添加系统变量。

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系统环境变量(可普遍访问的环境变量):

Windows system environment variable (Universally accessible environment variable):

setx API_KEY abcdefghijklmnopqrstuvwxyz123

Windows临时环境变量(运行此命令后调用的所有进程都可以访问该变量):

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

set API_KEY abcdefghijklmnopqrstuvwxyz123

为Linux中的所有进程添加环境变量更为复杂,请按照答案,在shell启动时自动设置环境变量。

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临时环境变量(运行此命令后调用的所有进程都可以访问该变量):

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

export API_KEY="abcdefghijklmnopqrstuvwxyz123"



测试环境变量



要测试环境变量集,可以使用以下命令(用变量名称代替 API_KEY):

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

如果这些返回的内容与您输入的内容相同,则它应该可以工作。您可以通过输入以下内容在python中进行尝试:

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.
>>> 

然后您可以编写python进行测试:

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.

这篇关于在环境Python / Django中存储配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆