'Flask - Webserver not reloading on code change
For some reason, 127.0.0.1:5000(port 5000) is stuck displaying my old un-updated file.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "Home page"
@app.route("/about")
def about():
return "About page"
if __name__ == "__main__":
app.run()
I changed the port and it worked fine. But, why is 5000 not updating when I change and run my code? I checked to see if multiple process were running, but none were.
Honestly, this is a noob question, but I can't seem to find anyone else who's had this problem.
Solution 1:[1]
Press CTR + C to end your server on the terminal and copy and paste this in the terminal to run Flask again
FLASK_DEBUG=1 flask run
This will set Debug mode: on
Solution 2:[2]
To set variable in powershell -
$env:FLASK_DEBUG=1
Solution 3:[3]
No! You have not run your app in debug mode.
app.run(debug=True)
This way any changes in your code will restart the server and your code changes will reflect pretty much in real time.
Solution 4:[4]
I don't know why this happens. Other answer didn't help. Community should help.
Other Two options:
If you are using Visual Studio Code, Close and Open it.
Uninstall and install python again.
Solution 5:[5]
Reload the page using Shift+ F5. But make sure app should be in debug mode i.e. app.run(debug=True).
Solution 6:[6]
If you are using the built-in app.run() try adding app.run(debug=True) It solves.
Solution 7:[7]
Somehow the Flask server froze. For me the solution was to kill the pythonw.exe process in the task manager.
Solution 8:[8]
For me, running these three commands took care of the reloading problem (windows machine):
$env:FLASK_APP = "main.py"
$env:FLASK_DEBUG = 1
flask run
Solution 9:[9]
To anyone still struggling with this issue while having debug=True correctly passed to app.run(), Flask (2.0.x) documentation states that :
debug=True can be passed to enable the debugger and reloader, but the FLASK_ENV=development environment variable is still required to fully enable development mode
See https://flask.palletsprojects.com/en/2.0.x/server/
So you should also run your development server with export FLASK_ENV=development before flask run.
Solution 10:[10]
My issue was that another application (Proton Mail Bridge) was listening on the same port (5000) my Flask app was, however no error was thrown. If you are on Windows, I would try running netstat -ab in CMD after opening as an administrator and see if any applications are listening to the same port your flask app is running on.
4.5 years later I know, but I could not find any answers on this earlier.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
