1
votes

Hi guys I'm getting error while trying to run Flask code. I'm doing a course from Udemy (the-python-mega-course): posting code and error below:

Code: from flask import Flask, render_template

    app=Flask(__name__)

    @app.route('/')
    def home():
        return render_template("home.html")

    @app.route('/about/')
     def about():
         return render_template("about.html")

    if __name__=="__main__":
        app.run(port=5000, debug=True)

Error: * Restarting with stat An exception has occurred, use %tb to see the full traceback.

SystemExit: 1

C:\Users\Vineet\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py:2889: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D. warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

Complete Traceback:

File "", line 11, in app.run(host='127.0.0.1', port=5000, debug=True)

File "C:\Users\Vineet\Anaconda3\lib\site-packages\flask\app.py", line 841, in run del _get_debug, _set_debug

File "C:\Users\Vineet\Anaconda3\lib\site-packages\werkzeug\serving.py", line 737, in run_simple serving.

File "C:\Users\Vineet\Anaconda3\lib\site-packages\werkzeug_reloader.py", line 265, in run_with_reloader import signal

SystemExit: 1

As I'm totally new to Flask framework any help appreciated.

Regards

2

2 Answers

3
votes

According to flask documentation the best way to start the server is using environment variables as follows and to use debug mode you can set FLASK_ENV variable to development. Therefore the final part of your code with

if __name__=="__main__": app.run(port=5000, debug=True)

is not a must to use if you use this method.

For Windows CMD:

set FLASK_APP=hello.py

flask run

Windows PowerShell:

$env:FLASK_APP = "hello.py"

flask run

-2
votes

The error is occurring because you might be running the code from Jupyter notebook. You should write the code in TEXT editor and execute it from python command prompt. For example, if your code is saved in a text file named Flask1.py. Use below code to run it from command shell.

pyhton Flask1.py

Now the server should start without error at localhost:port_number in the code or at 5000, the default port.