flask icon indicating copy to clipboard operation
flask copied to clipboard

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

Open yoavram opened this issue 10 years ago • 30 comments

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.

yoavram avatar May 29 '16 19:05 yoavram

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

$ python -m flask run

gledi avatar May 30 '16 05:05 gledi

This might fall under the same issue as #1847.

mitsuhiko avatar Jun 01 '16 23:06 mitsuhiko

There is a question on stackoverflow for this issue:

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

andersea avatar Jun 07 '16 19:06 andersea

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...

dgbillotte avatar Jun 20 '16 23:06 dgbillotte

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'

t0mhaines avatar Jun 30 '16 15:06 t0mhaines

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.

yoavram avatar Jul 01 '16 15:07 yoavram

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


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

C:\Users\Fer Ben\Documents\GitHub\flask>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\GitHub\flask>set FLASK_DEBUG=1

C:\Users\Fer Ben\Documents\GitHub\flask>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

ferbecab03 avatar Dec 09 '16 15:12 ferbecab03

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.

ferbecab03 avatar Dec 09 '16 15:12 ferbecab03

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.

mitsuhiko avatar Dec 09 '16 15:12 mitsuhiko

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?

ferbecab03 avatar Dec 09 '16 15:12 ferbecab03

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

mitsuhiko avatar Dec 09 '16 16:12 mitsuhiko

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

ferbecab03 avatar Dec 09 '16 16:12 ferbecab03

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

ferbecab03 avatar Dec 09 '16 17:12 ferbecab03

@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.

mitsuhiko avatar Dec 09 '16 17:12 mitsuhiko

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

yoavram avatar Dec 10 '16 07:12 yoavram

This should be fixed on the Werkzeug 0.11 branch.

mitsuhiko avatar Dec 10 '16 09:12 mitsuhiko

Is the issue then closed because of the new update?

ferbecab03 avatar Dec 12 '16 20:12 ferbecab03

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

mitsuhiko avatar Dec 12 '16 21:12 mitsuhiko

@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.

y2kbugger avatar Dec 15 '16 01:12 y2kbugger

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.

mitsuhiko avatar Dec 26 '16 18:12 mitsuhiko