Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Flask from IPython raises 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)

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
like image 637
user2906657 Avatar asked Mar 23 '18 18:03

user2906657


1 Answers

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)

In Visual Studio Code, to setup flask run in the launcher (instead of launch python), use this configuration in .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
    }
like image 189
davidism Avatar answered Sep 27 '22 20:09

davidism