从IPython运行Flask提升SystemExi

2024-09-28 20:54:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从IPython运行我的烧瓶应用程序。但是,它失败了,并出现一个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

Tags: runnameindebugappifusemain
1条回答
网友
1楼 · 发布于 2024-09-28 20:54:14

您正在使用Jupyter Notebook或IPython运行开发服务器。您还启用了调试模式,默认情况下启用了重新加载程序。重载程序尝试重新启动进程,但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)

相关问题 更多 >