Set Flask environment to development mode as default?
Asked Answered
S

4

42

Every time I start up my flask app the environment variable is set to production. I want to have it set to development mode by default. Otherwise every time I start my app i have to run ..

export FLASK_ENV=development

How can I set environment's default value as development in every startup?

EDIT: I am using flask in a virtual environment on a raspberry pi.

Smack answered 4/9, 2018 at 9:17 Comment(0)
A
57

You can edit your main flask app file and add these lines:

if __name__ == '__main__':
    app.run(debug=True)

Using this method you have to run your flask app with Python interpreter like this => python app.py

Best Practice:

  1. Install python-dotenv package inside your working environment =>pip install python-dotenv

  2. Create a file named .env, put your environment variables in it, for your case it's FLASK_ENV=development

  3. Then add this code to your config.py or some file that will get loaded before Flask main App

    from dotenv import load_dotenv
    dotenv_path = join(dirname(__file__), '.env')  # Path to .env file
    load_dotenv(dotenv_path)
    

Note that: If you are using flask command to run your application, you don't need to do the third step, flask will find .env files in the project directory by itself.

Using this method, it will only set Environment variable for the project that you have added this code to.

Ageratum answered 4/9, 2018 at 10:47 Comment(5)
Thanks! based on your idea I found an easier solution that fit my problem and just added FLASK_ENV=development to my .flaskenv file and that fixed the issue. I didnt need to create a config file, i assume flask is already configured to load the .flaskenv before the main module.Smack
Thanks @crazyPen. Also, don't forget to pointing your terminal to app.py & .flaskenv file location. Don't run your python from outside app.py location, e.g. python my_app/app.py. Otherwise it wouldn't read env setting.Farce
@FendiSetiawan It's not the app that reads the values, it's Flask, you can move it but you should tell the path to flask using FLASK_APP env variableAgeratum
.env.dev should be in the config directoryTurbofan
@Turbofan That hardly depends on how you structure your project, Flask does not enforce any type of structure, So it's totally up to the developer.Ageratum
W
9

On Linux distro, like "Raspberry pi o.s", specify the environment on the terminal with the code below. Unless you specify the environment, flask will assume production.

#Since version 2.3.0, Flask has replaced FLASK_ENV=development with FLASK_DEBUG=1
# For older installations use FLASK_ENV=development instead
export FLASK_DEBUG=1 

flask run
Wrench answered 4/9, 2018 at 9:22 Comment(0)
I
3

Like the first answer and instead of adding the variable to a .env file which can be forgotten, do this instead. This way, if you try to run the file in production, you'll get an assertion error to remind you to actually use a dedicated web server (which "imports" the app). If you run locally, not only will you be reminded to use a .env file, but in the case no environment file is needed, the flask env is set to development to avoid any production conflicts.

import os

app = Flask(__name__)
IS_DEV = app.env == 'development'  # FLASK_ENV env. variable

# code

if __name__ == '__main__':
    # guaranteed to not be run on a production server
    assert os.path.exists('.env')  # for other environment variables...
    os.environ['FLASK_ENV'] = 'development'  # HARD CODE since default is production
    app.run(debug=True)
Insurgence answered 20/8, 2021 at 22:26 Comment(0)
Y
2

Since version 2.3.0, Flask has replaced FLASK_ENV=development with FLASK_DEBUG=1

Youngs answered 4/3 at 19:9 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Sideslip

© 2022 - 2024 — McMap. All rights reserved.