1

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

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Hi ikuamike Thanks that works, but if i want to run it from spyder IDE then how to do it.
In your if statement try app.debug=True then app.run(port=5000, debug=True)
0

Use this it worked for me on jupyter

try: 
    if  __name__ == '__main__':
        app.run(debug=True,port=8000)
except:
    print("Exception occured!")
    from werkzeug.serving import run_simple
    run_simple('localhost', 9000, app)

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.