Set Flask environment to development mode as default?

后端 未结 2 486
[愿得一人]
[愿得一人] 2020-12-15 05:06

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

相关标签:
2条回答
  • 2020-12-15 05:35

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

    0 讨论(0)
  • 2020-12-15 05:36

    You can do this, unless you specify the enviroment, flask will assume production.

    export FLASK_ENV=development
    flask run
    
    0 讨论(0)
提交回复
热议问题