20

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.

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'
Vaibhav Mule
  • 5,016
  • 4
  • 35
  • 52

2 Answers2

38

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

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 ENV you set since the interpreter executes all the code in the class definitions before running the rest of the script.

Daniel Corin
  • 1,987
  • 2
  • 15
  • 27
  • Of course I specify the environment before the execution, but what I want is when I modify in dev. environment I do not want the prod either touch, even if i restart my server (as if it is two different directory). – Abdellah ALAOUI ISMAILI Jul 07 '17 at 08:22
14

To add onto Daniel's answer:

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

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

chintan-p-bhatt
  • 111
  • 1
  • 10
Nathan Wailes
  • 9,872
  • 7
  • 57
  • 95
  • The answer is 3years old and the current best option is to use `FLASK_ENV` config. Link here : https://flask.palletsprojects.com/en/1.1.x/config/#environment-and-debug-features – Aditya Shaw Mar 12 '21 at 03:02
  • @AdityaShaw not quite, it is still in the documentation here: https://flask.palletsprojects.com/en/2.0.x/config/#development-production "best option" is an opinion, but this is still a valid approach. – Jesse H. Jun 18 '21 at 18:52
  • @jesse-h without checking the environment variable, how can you know which config to load? (My first comment is wrong. I didn't mean to say it is wrong/old. The Daniel's answer is the right step to perform to know which mode you're in, imo.) – Aditya Shaw Jun 19 '21 at 19:12
  • I like this answer, however, it looks there might be conflicts with some configs, see https://stackoverflow.com/questions/71840211/how-to-use-flask-env-inside-configuration-objects – BorjaEst Apr 12 '22 at 09:30