Running Flask from IPython raises SystemExit

2020-02-07 03:39发布

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)

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

1条回答
该账号已被封号
2楼-- · 2020-02-07 03:43

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.

Preferably, use the flask command to run the development server.

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

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

Or disable the reloader if you want to call app.run from Jupyter.

app.run(debug=True, use_reloader=False)
查看更多
登录 后发表回答