Search code examples
pythonflask

Running Flask in Debug Mode


I am working on my first Flask app. All I am trying to do is to make sure that my Flask server restarts after I edit the hello.py file.

Here is my app structure: enter image description here

Here is hello.py:

from flask import Flask 

app = Flask(__name__)

@app.route("/") 
def hello(): 
    return "Hello World"    

and here is my .flask_env file:

FLASK_APP=hello.py
FLASK_DEBUG=True   

When I run the app in debug more using

flask run --debug

I get the following error:

Error: Could not locate a Flask application. Use the 'flask --app' option, 'FLASK_APP' environment variable, or a 'wsgi.py' or 'app.py' file in the current directory.

What am I missing?


Solution

  • The file name is .flaskenv and not .flask_env. That is explained in the documentation. You can also see that in the source code. That's why your flask env variables are not being read.

    ...
            if path or os.path.isfile(".env") or os.path.isfile(".flaskenv"):
    ...
    

    https://github.com/pallets/flask/blob/255c8d66af6daff3eaa063ea0819e185d4d1e214/src/flask/cli.py#L716