Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Flask environment to development mode as default?

Tags:

python

flask

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.

like image 587
crazyPen Avatar asked Sep 04 '18 09:09

crazyPen


People also ask

How do I change my environment from production to development mode in Flask?

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. Changed in version 1.0: Added FLASK_ENV to control the environment separately from debug mode.

How do I enable developer mode on Flask?

Command Line The flask run CLI command is the recommended way to run the development server. Use the --app option to point to your application, and the --debug option to enable debug mode.

How do I set environment variables in Flask?

Command-line: 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.


2 Answers

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 codes to..

like image 90
DarkSuniuM Avatar answered Sep 22 '22 07:09

DarkSuniuM


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.

export FLASK_ENV=development flask run 
like image 32
Kedarnag Mukanahallipatna Avatar answered Sep 20 '22 07:09

Kedarnag Mukanahallipatna