user3666820 user3666820 - 4 months ago 37
Python Question

Flask: Restarting with reloader can't find file

I'm trying to learn flask. I've created a virtual environment (venv) in my project directory (C:\users\ian\git\flasktutorial). FLASK_APP is set to flasktutorial, FLASK_DEBUG=1, and I do have a flasktutorial.py in the directory.

When I type "flask run" I get the following:

* Serving Flask app "flasktutorial"
* Forcing debug mode on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with windowsapi reloader
c:\users\ian\git\flasktutorial\venv\scripts\python.exe: can't open file 'C:\users\Ian\Git\flasktutorial\venv\Scripts\flask': [Errno 2] No such file or directory


Checking that folder, it does seem to have a flask.exe in it. Not sure what's going on here.

Answer

Run flask as a module, then the reloader will work on Windows:

python -m flask run

Explanation:

When starting the app in Windows with flask.exe then the reloader constructs wrong args for the new subprocess. Source code here.

It tries to run the python interpreter with flask.exe as script name, but without the extension, this is the the reason for the file not found error. If you make a dupe and copy Scripts/flask.exe to Scripts/flask then it will work. ;)

Otherwise, when running flask as a module then the sys.argv contains proper paths for the reloader to run the subprocess.

Comments