Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

python - Set Flask environment to development mode as default?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...