python - 从 IPython 运行 Flask 引发 SystemExit

标签 python flask ipython jupyter

我正在尝试从 IPython 运行我的 Flask 应用程序。但是,它失败并出现 SystemExit 错误。

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello, World!'

if __name__ == '__main__':
   app.run(debug=True)

使用 IPython 运行此命令会显示以下错误:

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 来运行开发服务器。您还启用了 Debug模式,默认情况下启用重新加载器。重新加载器尝试重新启动该进程,但 IPython 无法处理。

最好使用flask命令来运行开发服务器。

export FLASK_APP=my_app.py
export FLASK_DEBUG=1
flask run

如果您仍想使用 app.run,则可以使用普通的 python 解释器来运行应用程序,但不再推荐这样做。

python my_app.py

或者如果您想从 Jupyter 调用 app.run,请禁用重新加载器。

app.run(debug=True, use_reloader=False)

在 Visual Studio Code 中,要在启动器中设置 flask run(而不是启动 python),请在 .vscode/launch.json 中使用此配置:

    {
      "name": "Python: Flask",
      "type": "python",
      "request": "launch",
      "module": "flask",
      "env": { "FLASK_APP": "my_app.py", "FLASK_ENV": "development" },
      "args": ["run"],
      "args_": ["run", "--no-debugger"],      
      "jinja": true
    }

关于python - 从 IPython 运行 Flask 引发 SystemExit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58428825/

相关文章:

pandas - IPython/ Pandas : Is there an canonical way to detect rapid changes in a timeseries?

python - IPython 和 bpython 中的 Ctrl+D 等价物?

python - PySide 入门

python - HttpsCallable 从 python 返回

widget - 更改 IPython 笔记本小部件中标签的大小

flask - JSONAPI : Update relationships including attributes

python - Jinja for 循环范围在递增变量时被重置

python - 将 python WeakSet 提供给列表构造函数是否安全?

python - 有没有办法有效地计算两个(或更多)迭代器的乘积?

python - Pandas 倒计时功能的反向累积?