Having Error while running simple Python Flask web application
Question:
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:UsersVineetAnaconda3libsite-packagesIPythoncoreinteractiveshell.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:UsersVineetAnaconda3libsite-packagesflaskapp.py”, line 841, in run
del _get_debug, _set_debug
File “C:UsersVineetAnaconda3libsite-packageswerkzeugserving.py”, line 737, in run_simple
serving.
File “C:UsersVineetAnaconda3libsite-packageswerkzeug_reloader.py”, line 265, in run_with_reloader
import signal
SystemExit: 1
As I’m totally new to Flask framework any help appreciated.
Regards
Answers:
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
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.
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)
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:UsersVineetAnaconda3libsite-packagesIPythoncoreinteractiveshell.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:UsersVineetAnaconda3libsite-packagesflaskapp.py”, line 841, in run
del _get_debug, _set_debug
File “C:UsersVineetAnaconda3libsite-packageswerkzeugserving.py”, line 737, in run_simple
serving.
File “C:UsersVineetAnaconda3libsite-packageswerkzeug_reloader.py”, line 265, in run_with_reloader
import signal
SystemExit: 1
As I’m totally new to Flask framework any help appreciated.
Regards
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
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.
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)