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.
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.
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.
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.
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:
python-dotenv
package inside your working environment =>pip install python-dotenv
.env
, put your environment variables in it, for your case it's FLASK_ENV=development
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..
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With