Python – Having Error while running simple Python Flask web application

flaskpython

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

Best Answer

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

Related Topic