从IPython运行Flask会引发SystemExit [英] Running Flask from IPython raises SystemExit
问题描述
我正在尝试从IPython运行我的Flask应用程序.但是,它失败并显示SystemExit
错误.
I am trying to run my Flask application from IPython. However, it fails with a SystemExit
error.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)
使用IPython运行它会显示以下错误:
Running this with IPython shows the following error:
SystemExit Traceback (most recent call last)
<ipython-input-35-bfd7690b11d8> in <module>()
17
18 if __name__ == '__main__':
---> 19 app.run(debug = True)
/Users/ravinderbhatia/anaconda/lib/python2.7/site-packages/flask/app.pyc in run(self, host, port, debug, **options)
770 options.setdefault('use_debugger', self.debug)
771 try:
--> 772 run_simple(host, port, self, **options)
773 finally:
774 # reset the first request information if the development server
/Users/ravinderbhatia/anaconda/lib/python2.7/site-packages/werkzeug/serving.py in run_simple(hostname, port, application, use_reloader, use_debugger, use_evalex, extra_files, reloader_interval, reloader_type, threaded, processes, request_handler, static_files, passthrough_errors, ssl_context)
687 from ._reloader import run_with_reloader
688 run_with_reloader(inner, extra_files, reloader_interval,
--> 689 reloader_type)
690 else:
691 inner()
/Users/ravinderbhatia/anaconda/lib/python2.7/site-packages/werkzeug/_reloader.py in run_with_reloader(main_func, extra_files, interval, reloader_type)
248 reloader.run()
249 else:
--> 250 sys.exit(reloader.restart_with_reloader())
251 except KeyboardInterrupt:
252 pass
SystemExit: 1
推荐答案
您正在使用Jupyter Notebook或IPython运行开发服务器.您还启用了调试模式,默认情况下会启用重新加载程序.重新加载程序尝试重新启动IPython无法处理的过程.
You're using Jupyter Notebook or IPython to run the development server. You've also enabled debug mode, which enables the reloader by default. The reloader tries to restart the process, which IPython can't handle.
最好使用flask
命令运行开发服务器.
Preferably, use the flask
command to run the development server.
export FLASK_APP=my_app.py
export FLASK_DEBUG=1
flask run
或者如果您仍然想使用app.run
,则使用普通的python
解释器来运行该应用程序,这不再建议.
Or use the plain python
interpreter to run the application if you still want to use app.run
, which is no longer recommended.
python my_app.py
如果要从Jupyter调用app.run
,请禁用重新加载器.
Or disable the reloader if you want to call app.run
from Jupyter.
app.run(debug=True, use_reloader=False)
这篇关于从IPython运行Flask会引发SystemExit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!