Flask import problem with Python 3 and __main__.py

Jon Ribbens jon+usenet at unequivocal.co.uk
Tue Aug 26 12:03:04 EDT 2014


Flask suggests the following file layout:

    runflaskapp.py
    flaskapp/
        __init__.py

runflaskapp.py contains:

    from flaskapp import app
    app.run(debug=True)

flaskapp/__init__.py contains:

    from flask import Flask
    app = Flask(__name__)

Running this with 'python3 runflaskapp.py' works fine. However it
seems to me that a more Python3onic way of doing this would be to
rename 'runflaskapp.py' as 'flaskapp/__main__.py' and then run
the whole thing as 'python3 -m flaskapp'. Unfortunately this doesn't
work:

    $ python3 -m flaskapp
     * Running on http://127.0.0.1:5000/
     * Restarting with reloader
    Traceback (most recent call last):
      File "/home/username/src/flaskapp/__main__.py", line 1, in <module>
	from flaskapp import app
    ImportError: No module named 'flaskapp'

Does anyone know why and how to fix it?



More information about the Python-list mailing list