pex: Pex does not play nicely with Flask's `debug=True`
Given app.py:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(debug=True)
Run pex flask -o flask.pex, then ./flask.pex app.py:
❯ ./flask.pex app.py
* 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
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
Traceback (most recent call last):
File "/Users/eric/.pyenv/versions/3.9.1/lib/python3.9/runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/Users/eric/.pyenv/versions/3.9.1/lib/python3.9/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/Users/eric/code/debug/pex-flask-debug/app.py", line 1, in <module>
from flask import Flask
ModuleNotFoundError: No module named 'flask'
But if setting debug=False, then it works like normal. Here, it’s complaining about flask, but in all reported cases of Pants users, the issue was instead their first party code.
A user found last week that with Dash—a wrapper around Flask—they could set debug=True, use_reloader=False and things would work. So, we can be fairly confident the file reloader is what causes issues. Why is this?
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 16 (12 by maintainers)
OK - reproed via docker
python:3.9. Thabnks @Eric-Arellano and @gshuflin. It turns out my box has requests installed in the underlying interpreter, so when Flask restarts and busts out of the PEX it sees the underlying interpreter’s requests installation.I debugged with a slightly modified app.py:
In the
python:3.9image I repro:This is fixed though by the
--venvmode on master that will be released in 2.1.25:So … I’m going to keep this as question / answered and closed. It’s still the case that no zipapp works with Flask but Pex (upon next release) provides a workaround.
I’m going to close this issue as answered since Pex fails just like a standard zipapp but also gives you a workaround with
--unzip.