Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python virtual environment, can't find '__main__' module in

Tags:

python

flask

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?

like image 506
fabiano.mota Avatar asked Sep 02 '25 15:09

fabiano.mota


1 Answers

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.

like image 76
djromero Avatar answered Sep 05 '25 15:09

djromero