简体   繁体   English

Flask app config.py vs dotenv 访问环境变量

[英]Flask app config.py vs dotenv to access environment variables

Hi I am relatively new to programming and building my first flask project and I haven't been able to figure out if I should prefer accessing environment variables by using dotenv / load_dotenv or using them from a config.py file.嗨,我对编程和构建我的第一个 flask 项目比较陌生,我一直无法弄清楚我是应该更喜欢通过使用dotenv / load_dotenv还是从 config.py 文件中使用它们来访问环境变量。

I understand the config route is more flexible but my question is specifically to do with environment variables.我知道配置路由更灵活,但我的问题专门与环境变量有关。

Is there a best practice here?这里有最佳实践吗? [I am building a simple app that will be hosted externally] [我正在构建一个将在外部托管的简单应用程序]

Best practices dictate that any value which is secret should not be hard-coded into any files which persist with the project or are checked into source control.最佳实践规定,任何秘密值都不应该被硬编码到项目中持续存在或签入源代码管理的任何文件中。 Your config file is very likely to be saved in source control, so it should not store secrets, but instead load them from the environment variables set at execution time of the app.您的配置文件很可能保存在源代码管理中,因此它不应存储秘密,而应从应用程序执行时设置的环境变量中加载它们。 For example, let's say you are configuring an SMTP relay:例如,假设您正在配置 SMTP 中继:

MAIL_PORT is a value that is not secret and not likely to change so it is a good candidate to be set in your config file. MAIL_PORT是一个非机密且不太可能更改的值,因此很适合在您的配置文件中进行设置。

MAIL_PASSWORD is a secret value that you do not want saved in your project's repository, so it should be loaded from the host's environment variables. MAIL_PASSWORD是一个您不想保存在项目存储库中的秘密值,因此应该从主机的环境变量中加载它。

In this example, your config file might contain entries that look something like this:在此示例中,您的配置文件可能包含如下所示的条目:

MAIL_PORT = 465
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')

Beyond evaluating whether or not a config value is a secret, also consider how often the value will change and how hard it would be to make that change.除了评估配置值是否是秘密之外,还要考虑该值更改的频率以及进行更改的难度。 Anything hard-coded into your config file will require changing the file and adding a new commit to your source control, possibly even triggering a full CI/CD pipeline process.任何硬编码到配置文件中的内容都需要更改文件并向源代码管理添加新的提交,甚至可能触发完整的 CI/CD 管道过程。 If the value were instead loaded from environment variables then this value could be changed by simply stopping the application, exporting the new value as an environment variable, and restarting the application.如果该值是从环境变量加载的,则可以通过简单地停止应用程序、将新值导出为环境变量并重新启动应用程序来更改该值。

Dotenv files are simply a convenience for grouping a number of variables together and auto-loading them to be read by your configuration. Dotenv 文件只是将多个变量组合在一起并自动加载它们以供您的配置读取的便利工具。 A .env file is not always used as these values can be manually exported when the app is invoked or handled by another system responsible for starting or scaling your application.并不总是使用.env文件,因为当应用程序被另一个负责启动或缩放应用程序的系统调用或处理时,可以手动导出这些值。 Do not check .env or .flaskenv files into your source control.不要将.env.flaskenv文件签入源代码管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM