Flask: "[Errno 2] No such file or directory" on flask run with FLASK_DEBUG=1

Created on 29 May 2016  ·  30Comments  ·  Source: pallets/flask

I get the following [Errno 2] No such file or directory with flask 0.11 & werkzeug 0.11.10 on Windows 7 64bit with Python 3.5 but only when FLASK_DEBUG=1.

D:\tmp
λ python --version
Python 3.5.1 :: Continuum Analytics, Inc.

D:\tmp
λ python -c "import hello_flask"

D:\tmp
λ set FLASK_APP=hello_flask.py

D:\tmp
λ flask run
 * Serving Flask app "hello_flask"
 * Forcing debug mode off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

D:\tmp
λ set FLASK_DEBUG=1

D:\tmp
λ flask run
 * Serving Flask app "hello_flask"
 * Forcing debug mode on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with windowsapi reloader
d:\anaconda3\envs\flask\python.exe: can't open file 'D:\Anaconda3\envs\flask\Scripts\flask': [Errno 2] No such file or directory

hello_flask.py is the minimal app from the docs.

bug cli

Most helpful comment

I faced the same error. However it works when I run it as a module:

$ python -m flask run

All 30 comments

I faced the same error. However it works when I run it as a module:

$ python -m flask run

This might fall under the same issue as #1847.

There is a question on stackoverflow for this issue:

http://stackoverflow.com/questions/37650208/flask-cant-find-app-file

I'm having the same problem on Windows Server with Flask 0.11.1 except when I try

$ python -m flask run

I get: ImportError: cannot import name Flask

Currently I can only make it work with FLASK_DEBUG=0

I'd be happy to provide any other details...

This is caused as python is looking for a script named flask, but on windows it is called flask.exe.

As a temporary fix you can copy and paste flask.exe, and just rename it 'flask'

Good call @TomIsPrettyCoool.
So this is a bug in the reloader - note that windows finds flask when prompted to, although it's flask.exe. the werkzeug reloader probably calls flask instead of flask.exe.
I imagine there are two ways to solve this: (1) fix the reloader to use flask.exe on windows; (2) add batch file called flask next to flask.exe during flask install. (2) is probably easier and contained in flask, but (1) might solve future bugs for other packages.

I am unable to currently replicate the bug as it uses stat instead of the windowsapi reloader.

```

C:\Users\Fer Ben\Documents\GitHubflask>set FLASK_DEBUG=0

C:\Users\Fer Ben\Documents\GitHubflask>flask run

  • Serving Flask app "hello_flask"
  • Forcing debug mode off
  • Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

C:\Users\Fer Ben\Documents\GitHubflask>set FLASK_DEBUG=1

C:\Users\Fer Ben\Documents\GitHubflask>flask run

  • Serving Flask app "hello_flask"
  • Forcing debug mode on
  • Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
  • Restarting with stat
  • Debugger is active!
  • Debugger pin code: 183-411-740

It seems it doesn't happen with the new updated version of werkzeug since it uses the stat reloader if you don't have the watchdog package installed.

If you run into this you can temporarily use python -mflask. I encountered the same thing recently. Need to see what this is. I think it's that it looks for flask but it should look for flask.exe.

I am trying to replicate the bug to see if if implementing the batch file fix was feasible. However, I am having difficulty trying to get the error to come up. Any suggestions?

I saw it happen on Python 3 in a virtualenv. Not sure if that's the key.

It could be. Currently running Python 3.5.2 and also an updated werkzeug.

@mitsuhiko What would you propose to follow up on next? Since the new werkzeug changed the reloader.

@ferbecab03 fix it in werkzeug. Btw I wonder if the issue also happens if you run it as "flask.exe" instead of "flask" from the command line.

I can't help with this - no longer using a Windows machine...

This should be fixed on the Werkzeug 0.11 branch.

Is the issue then closed because of the new update?

I need to release it still. Was kinda hoping someone would test this on windows since I'm not with a windows machine currently.

@ALL I have noticed that is is sufficient to use --reload to reproduce the bug as well

@mitsuhiko - the fix needed some tweaking an might need something more (or there could be an unrelated issue)

Firstly, I noticed that the fix checked for 'NT', but on my system at least, os.name returns 'nt', I've updated this to handle either case.

After more digging I notice that your .exe fix was being applied to the wrong arg .../python.exe rather than .../flask.

Here is a working fix:
https://github.com/y2kbugger/werkzeug/commit/cb0eb473ce8e1583d9521b2ef301fd4c0f05dd23

HOWEVER...
At first I didn't realize that it was in fact reloading and serving correctly because now I get this long traceback and error on each reload.

Traceback (most recent call last):
  File "c:\anaconda2\envs\pytread\lib\threading.py", line 914, in _bootstrap_inner
    self.run()
  File "c:\anaconda2\envs\pytread\lib\site-packages\watchdog\observers\api.py", line 146, in run
    self.queue_events(self.timeout)
  File "c:\anaconda2\envs\pytread\lib\site-packages\watchdog\observers\read_directory_changes.py", line 77, in queue_eve nts
    winapi_events = read_events(self._handle, self.watch.is_recursive)
  File "c:\anaconda2\envs\pytread\lib\site-packages\watchdog\observers\winapi.py", line 347, in read_events
    buf, nbytes = read_directory_changes(handle, recursive)
  File "c:\anaconda2\envs\pytread\lib\site-packages\watchdog\observers\winapi.py", line 307, in read_directory_changes
    raise e
  File "c:\anaconda2\envs\pytread\lib\site-packages\watchdog\observers\winapi.py", line 303, in read_directory_changes
    ctypes.byref(nbytes), None, None)
  File "c:\anaconda2\envs\pytread\lib\site-packages\watchdog\observers\winapi.py", line 108, in _errcheck_bool
    raise ctypes.WinError()
OSError: [WinError 87] The parameter is incorrect.

This seems to be a separate outstanding issue with the watchdog package. Watchdog used when installed or else werkzeug falls back stats. https://github.com/gorakhargosh/watchdog/issues/296
I DO NOT get this error when running as python -m flask run so it must be avoidable, but I cannot figure it out.

This is now fixed in Werkzeug 0.11.13. Sorry it took a while but I did not have a windows PC with me until now.

I am still getting the same behavior, when trying to run a packaged application, without installing and also using the FLASK_DEBUG=1 ... :confused:

Inside a virtualenv using Ubuntu 16.04.3 LTS

pip list output:

Package      Version
------------ -------
click        6.7
Flask        0.12.2
gunicorn     19.7.1
itsdangerous 0.24
Jinja2       2.9.6
MarkupSafe   1.0
pip          9.0.1
setuptools   36.5.0
Werkzeug     0.12.2
wheel        0.30.0

Then python -m flask run works ...
FLASK_APP=myapp FLASK_DEBUG=1 python -m flask run --host 0.0.0.0

but flask run does not ...
FLASK_APP=myapp FLASK_DEBUG=1 flask run --host 0.0.0.0

 * Serving Flask app "myapp"
 * Forcing debug mode on
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 119-502-443
195.xxx.xxx.xxx - - [20/Oct/2017 15:11:16] "GET / HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/d2/home/dimitris/webapps/myapp/lib/python2.7/site-packages/flask/cli.py", line 184, in __call__
    self._flush_bg_loading_exception()
  File "/d2/home/dimitris/webapps/myapp/lib/python2.7/site-packages/flask/cli.py", line 172, in _flush_bg_loading_exception
    reraise(*exc_info)
  File "/d2/home/dimitris/webapps/myapp/lib/python2.7/site-packages/flask/cli.py", line 161, in _load_app
    self._load_unlocked()
  File "/d2/home/dimitris/webapps/myapp/lib/python2.7/site-packages/flask/cli.py", line 176, in _load_unlocked
    self._app = rv = self.loader()
  File "/d2/home/dimitris/webapps/myapp/lib/python2.7/site-packages/flask/cli.py", line 237, in load_app
    rv = locate_app(self.app_import_path)
  File "/d2/home/dimitris/webapps/myapp/lib/python2.7/site-packages/flask/cli.py", line 100, in locate_app
    'ensure the extension is .py' % module)
NoAppException: The file/path provided (myapp) does not appear to exist.  Please verify the path is correct.  If app is not on PYTHONPATH, ensure the extension is .py

I too am getting the same problem.

(venv) ~/c/t/flask-tutorial ❯❯❯ flask run
Usage: flask run [OPTIONS]

Error: The file/path provided (flaskr) does not appear to exist.  Please verify the path is correct.  If app is not on PYTHONPATH, ensure the extension is .py

The fix suggested worked but it took me along time to find the suggestion.

venv) ~/c/t/flask-tutorial ❯❯❯ python -m flask run                                                                 ⏎
 * Serving Flask app "flaskr" (lazy loading)
 * Environment: development
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 109-653-199

My pip list FWIW

click (6.7)
Flask (1.0.1)
itsdangerous (0.24)
Jinja2 (2.10)
MarkupSafe (1.0)
pip (9.0.1)
setuptools (28.8.0)
Werkzeug (0.14.1)

What is the equivalent of 'python -m flask run' if I am importing the python flask library and using

import flask from Flask
app = Flask(__name__)
app.run(host='0.0.0.0', port=8000, debug=True)

Currently Im getting the same error can't open file '<>/start': [Errno 2] No such file or directory and Im on [email protected]

If you want to run the CLI programatically, it would be:

from flask.cli import main
main(["--port", "8080"])

Your example suggests you're trying to use the development server in production (binding to the public with 0.0.0.0). Do not do this, use production WSGI and HTTP servers, see the deployment docs.

No Im trying to run the server locally and have the server update with changes every time I save a file during development which is why Im trying to use debug=True which is not working... I dont think your answer addresses that issue...

Do export FLASK_ENV=development before flask run. This, and the rest of the CLI behavior, is documented here: https://flask.palletsprojects.com/en/1.1.x/cli/#environments

And don't put app.run in your source file.

On Fri, Oct 18, 2019 at 1:34 PM David Lord notifications@github.com wrote:

Do export FLASK_ENV=development before flask run. This is documented
here: https://flask.palletsprojects.com/en/1.1.x/cli/#environments


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/pallets/flask/issues/1829?email_source=notifications&email_token=ABORVLLECDJDURP7SANATHDQPHXSRA5CNFSM4CFFNJK2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBVIHHY#issuecomment-543851423,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/ABORVLJ4K4VEVOLEJIPBPBTQPHXSRANCNFSM4CFFNJKQ
.

Im using nginx for the web server. But when you say "use production WSGI" what do you mean because I see in the documentation it says "Just remember that your Flask application object is the actual WSGI application." So what more do I need to do in terms of WSGI?

in my case due to carriage return, i need to convert \r\n to \n (LF) and it works

Was this page helpful?
0 / 5 - 0 ratings