I am studying Flask,with my first book example I have encountered problem
>>> from flask import Flask
>>> app = Flask(__name__)
>>> @app.route('/')
... def index():
... return '<h1>Hello World!</h1>'
...
>>> if __name__ == '__main__':
... app.run(debug=True)
...
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
/home/tati/flasky/venv/bin/python: can't find '__main__' module in ''
(venv) tati@tati-System-Product-Name:~/flasky$ python --version
Python 2.7.12
My Python venv is 2.7.Does this creates havoc or not? If I go out,from command line(conda python installation)
python --version
Python 3.6.0 :: Anaconda custom (64-bit)
How can I solve this?
The problem is that with debug=True
flask will relaunch the python script to run an additional thread with the reloader. When running from the console it can't find any script because there isn't any.
You can work around this removing debug=True
.
Also, in the console you just call whatever needs to be called, no need for if __name__ == '__main__'
idiom; there is no alternative code path.
Anyway, the console is not the best option to learn Flask. Soon enough you'll need somewhere to put the templates and additional files like blueprints, etc.
The best option in my opinion is to create a package for your study app. Just create a directory with a __init__.py
file and an additional one app.py
where you put the tutorial code. Remember to adjust your PYTHONPATH accordingly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With