Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set FLASK_ENV inside config file?

Tags:

python

flask

TL;DR

I don't want to export my variables every time I log on. I want them in my config.

From the docs

Using the environment variables as described above is recommended. While it is possible to set ENV and DEBUG in your config or code, this is strongly discouraged.

How do I do it anyway? How is it 'possible'?

MORE - What I've Tried

I have a flask.cfg with FLASK_ENV='development'. I run flask run and I get development printed. But the production server has already started. terminal It ignores the following:

app.config.from_pyfile("flask.cfg")

print(f"{app.config['ENV']}") => development

This is in a personal project of no consequence. That's why I am ignoring best practices for convenience.

like image 495
Mote Zart Avatar asked Feb 08 '19 21:02

Mote Zart


People also ask

How to configure a flask environment?

Flask has a config attribute by default, it’s a dictionary that you can access and set the configuration to the “flask environment” But this is not the only way, you don’t need to set multiple times like: You can use a method called “ update ”. So we will basically have an initial setup to our project like this:

What are ENV and debug config values in flask?

This gives you defaults that can be overridden from environment variables. The ENV and DEBUG config values are special because they may behave inconsistently if changed after the app has begun setting up. In order to set the environment and debug mode reliably, Flask uses environment variables.

What is the difference between Flask_app and flask_env?

It looks as if the FLASK_APP variable incorporates the 2 lines of code that we avoided. Similarly, the FLASK_ENV variable sets the environment on which we want our flask application to run. For example, if we put FLASK_ENV=development the environment will be switched to development. By default, the environment is set to development.

How do I run flask in debug mode on Windows?

To switch Flask to the development environment and enable debug mode, set FLASK_ENV: > $ export FLASK_ENV=development > $ flask run (On Windows, use set instead of export.) Using the environment variables as described above is recommended. While it is possible to set ENV and DEBUG in your config or code, this is strongly discouraged.


3 Answers

What about this: install python-dotenv package, create a .flaskenv file in your project root folder and add, for example, this:

FLASK_APP=app.py (or whatever you named it)
FLASK_ENV=development (or production)

Save. Do flask run.

like image 168
no use for a name Avatar answered Oct 13 '22 23:10

no use for a name


If you move your config into Python, things get a little easier. Consider

from flask import Flask
from config import Config

app = Flask(__name__)
app.config.from_object(Config)

where config.py looks like

import os

class Config:
    SECRET_KEY = os.environ.get('SECRET_KEY', 'default sekret')

This gives you defaults that can be overridden from environment variables.

like image 37
Dave W. Smith Avatar answered Oct 13 '22 22:10

Dave W. Smith


Update march 2020:

According to the Flask devs, you cannot do this anymore:

The ENV and DEBUG config values are special because they may behave inconsistently if changed after the app has begun setting up. In order to set the environment and debug mode reliably, Flask uses environment variables.

The environment is used to indicate to Flask, extensions, and other programs, like Sentry, what context Flask is running in. It is controlled with the FLASK_ENV environment variable and defaults to production.

Setting FLASK_ENV to development will enable debug mode. flask run will use the interactive debugger and reloader by default in debug mode. To control this separately from the environment, use the FLASK_DEBUG flag.

To switch Flask to the development environment and enable debug mode, set FLASK_ENV:

> $ export FLASK_ENV=development 
> $ flask run (On Windows, use set instead of export.)

Using the environment variables as described above is recommended. While it is possible to set ENV and DEBUG in your config or code, this is strongly discouraged. They can’t be read early by the flask command, and some systems or extensions may have already configured themselves based on a previous value.

like image 23
Bogdan Doicin Avatar answered Oct 13 '22 22:10

Bogdan Doicin