Stephen
Stephen

Reputation: 8840

FLASK_ENV seems to be ignored (can't enter debug environment)

According to the Flask documentation,

The flask script is nice to start a local development server, but you would have to restart it manually after each change to your code. That is not very nice and Flask can do better. If you enable debug support the server will reload itself on code changes, and it will also provide you with a helpful debugger if things go wrong.

To enable all development features (including debug mode) you can export the FLASK_ENV environment variable and set it to development before running the server:

$ export FLASK_ENV=development 
$ flask run

However, in my very simple example, code changes still don't take effect until I restart the server. I've set up the particular script I want to run with export FLASK_APP=hello.py and the script is as follows:

from flask import Flask, url_for, request, render_template
app = Flask(__name__)

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

While the Flask development server is running, I change the return value by adding or removing an exclamation mark and saving the file. Then I refresh the page on http://127.0.0.1:5000/ in Chrome, but the number of exclamation points is unchanged. Once I quit Flask in the terminal using Ctrl-C and restart it and then refresh the page again, I get the proper number of exclamation points.

This is on a Mac, Python 3.6.0 (Anaconda), Flask 0.12.

Am I misunderstanding how the development server can help me, or is there anything else you think I should check? I'm quite new to Flask.

Upvotes: 7

Views: 10879

Answers (3)

flomaster
flomaster

Reputation: 1812

Looking at app.config contents I discovered variable ENV='production'.

Set ENV='development' in config file and it worked. FLASK_ENV variable does not even exist.

Upvotes: 2

SakenMetOcean
SakenMetOcean

Reputation: 31

$env:FLASK_ENV = "development"
flask run

That's worked for me on my Win10 laptop running VS code

Upvotes: 3

Dave W. Smith
Dave W. Smith

Reputation: 24966

Try

FLASK_APP=app.py FLASK_DEBUG=1 TEMPLATES_AUTO_RELOAD=1 flask run

FLASK_DEBUG will get you the behavior you're looking for now; TEMPLATE_AUTO_RELOAD will get you the behavior you'll need as soon as you starting using templates.

Upvotes: 11

Related Questions