Starting my application results in a crash immediately after startup. It isn't clear what the error could mean, as it has just loaded the module it claims not to find.
It might be a red-herring, but why would it immediately restart?
Most other references I can find to this error message are when the file has been called as a script and not - as I already do - as a module (-m)
(.virtualenv3) rob@positron:~/Projects/<my_proj>/codebase/src$ source ../../.webapp.env && python3 -m webapp.main
* Serving Flask app "main" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
INFO:werkzeug: * Running on http://0.0.0.0:8002/ (Press CTRL+C to quit)
INFO:werkzeug: * Restarting with stat
/home/rob/Projects/<myproj>/.virtualenv3/bin/python3: Error while finding module specification for 'webapp.main' (ModuleNotFoundError: No module named 'webapp')
Edit 1: Here is some output to show that the calling method certainly works with python modules in the same package (albeit a basic test)
(.virtualenv3) rob@positron:~/Projects/<myproj>/codebase/src$ ls -la webapp/*.py
-rw-r--r-- 1 rob rob 0 Sep 9 15:37 webapp/__init__.py
-rw-r--r-- 1 rob rob 11494 Dec 13 13:19 webapp/main.py
-rw-r--r-- 1 rob rob 19 Dec 13 13:36 webapp/test.py
(.virtualenv3) rob@positron:~/Projects/<myproj>/codebase/src$ python -m webapp.test
Hello SO
(.virtualenv3) rob@positron:~/Projects/<myproj>/codebase/src$ python -m webapp.main
* Serving Flask app "main" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
INFO:werkzeug: * Running on http://0.0.0.0:8812/ (Press CTRL+C to quit)
INFO:werkzeug: * Restarting with stat
/home/rob/Projects/<myproj>/.virtualenv3/bin/python: Error while finding module specification for 'webapp.main' (ModuleNotFoundError: No module named 'webapp')
Edit 2. PYTHONPATH=$(pwd) python -m ....
makes it work, but why is this needed? just to get werkzeug to play nice?
EDIT 3. Diagnosed Flask is changing the CWD of the process.
Here's a test file (webapp/test.py
)
import os
print('cwd: ', os.getcwd())
from flask import Flask
Flask(__name__).run(port=9991, debug=True)
Watch the output of os.getcwd()
change when werkzeug in debug mode reloads the app
(.virtualenv3) rob@positron:~/Projects/<myproj>/codebase/src$ PYTHONPATH=`pwd` FLASK_ENV=development python -m webapp.test
cwd: /home/rob/Projects/<myproj>/codebase/src
* Serving Flask app "test" (lazy loading)
* Environment: development
* Debug mode: on
* Running on http://127.0.0.1:9991/ (Press CTRL+C to quit)
* Restarting with stat
cwd: /home/rob/Projects/<myproj>
* Debugger is active!
* Debugger PIN: 134-807-518
This is because "if" you have dotenv available, Flask will run it and the assume the location of the .env file found is the root of the project and switches to that.
Thanks Flask. No fix yet though.
https://github.com/pallets/flask/blob/master/src/flask/cli.py#L607
Add FLASK_SKIP_DOTENV=1
to skip the os.chdir()