Python 烧瓶生产开发模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44951244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:39:40  来源:igfitidea点击:

flask production and development mode

pythonflaskconfigdevelopment-environmentproduction-environment

提问by Abdellah ALAOUI ISMAILI

I have developed an application with flask, and I want to publish it for production, but I do not know how to make a separation between the production and development environment (database and code), have you documents to help me or code. I specify in the config.py file the two environment but I do not know how to do with.

我用flask开发了一个应用,想发布到生产环境中,但是不知道生产环境和开发环境如何分离(数据库和代码),有没有文档帮助我或者代码。我在 config.py 文件中指定了这两个环境但我不知道如何处理。

class DevelopmentConfig(Config):
    """
    Development configurations
    """
    DEBUG = True
    SQLALCHEMY_ECHO = True
    ASSETS_DEBUG = True
    DATABASE = 'teamprojet_db'
    print('THIS APP IS IN DEBUG MODE. YOU SHOULD NOT SEE THIS IN PRODUCTION.')


class ProductionConfig(Config):
    """
    Production configurations
    """
    DEBUG = False
    DATABASE = 'teamprojet_prod_db'

采纳答案by Nathan Wailes

To add onto Daniel's answer:

添加丹尼尔的回答:

Flask has a page in its documentation that discusses this very issue.

Flask 在其文档中有一个页面讨论了这个问题。

Since you've specified your configuration in classes, you would load your configuration with app.config.from_object('configmodule.ProductionConfig')

由于您已经在类中指定了配置,因此您可以使用 app.config.from_object('configmodule.ProductionConfig')

回答by Daniel Corin

One convention used is to specify an environment variable before starting your application.

使用的一种约定是在启动应用程序之前指定环境变量。

For example

例如

$ ENV=prod; python run.py

In your app, you check the value of that environment variable to determine which config to use. In your case:

在您的应用程序中,您检查该环境变量的值以确定要使用的配置。在你的情况下:

run.py

run.py

import os
if os.environ['ENV'] == 'prod':
    config = ProductionConfig()
else:
    config = DevelopmentConfig()

It is also worth noting that the statement

还值得注意的是,该声明

print('THIS APP IS IN DEBUG MODE. YOU SHOULD NOT SEE THIS IN PRODUCTION.')

prints no matter which ENVyou set since the interpreter executes all the code in the class definitions before running the rest of the script.

无论ENV您设置哪个都打印,因为解释器在运行脚本的其余部分之前执行类定义中的所有代码。