>> from fl...">

Python virtual environment, cannot find module "__main__" in

I am learning Flask, with my first example book I ran into a 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 it create havoc or not? If I exit the command line (installing conda python)

python --version
Python 3.6.0 :: Anaconda custom (64-bit)

      

How can I solve this?

+3


source to share


1 answer


The problem is that with debug=True

flask will restart the python script to start an additional thread with a restart. When run from the console, it cannot find the script because it is not there. You can bypass this removal debug=True

.

Also, in the console, you are calling whatever needs to be called, not needed for if __name__ == '__main__'

idiom; there is no alternative code path.



In any case, the console is not the best option for learning Flask. Soon enough, you will need to place templates and additional files such as blueprints, etc.

The best option, in my opinion, is to create a package for your sample application . Just create a directory with a file __init__.py

and another one app.py

where you put the tutorial code. Remember to adjust the PYTHONPATH accordingly.

+2


source







All Articles