1

enter image description here

I'm trying to restructure a flask app to a package based on http://flask.pocoo.org/docs/0.10/patterns/packages/. My app is based on http://code.tutsplus.com/tutorials/intro-to-flask-signing-in-and-out--net-29982 . I've now changed it to the screenshot above and am trying to run at command line.

runserver.py:

from app import intro_to_flask

if __name__ == '__main__':
    intro_to_flask.routes.run(debug=True)

At the command line:

/mini/app (master) $ python runserver.py
Traceback (most recent call last):
  File "runserver.py", line 1, in <module>
    from app import intro_to_flask
ImportError: No module named app

What am I doing wrong?

user1592380
  • 34,265
  • 92
  • 284
  • 515

2 Answers2

1

I believe you're looking for from intro_to_flask import app if I read the links you posted correctly, and assuming you've setup __init__.py correctly inside the intro_to_flask folder.

tryexceptpass
  • 529
  • 5
  • 14
  • I believe the init.py is in the right location, please see screenshot. I'm confused though , why no need to import the outer module 'app' ? – user1592380 Jan 28 '16 at 21:33
  • you've named your package app. intro_to_flask is the module, the `app` he is referring to is the `Flask()` initialization usually something like `app = Flask(__name__)` – Busturdust Jan 29 '16 at 00:42
  • In order to import anything from the intro_to_flask folder from your runserver.py file, python needs to know that the folder is a module it can import, and in order to do so that folder needs an __init__.py file as well. The link you posted has details on what should be in that file. – tryexceptpass Jan 30 '16 at 03:16
1

Since the Flask documentation mentions only one __init__.py file in the subdirectory with the Flask application and you have two of them, I think you're confused by the __init__.py file and the usage of imports. In your case the subdirectory with the Flask application is intro_to_flask.

Basically it works like this: from <module> import <object>

The module is a .py file and the object is defined inside the module. In this special case there is a __init__.py file so the module you have to reference to has the name of the directory that contains the __init__.py file.

Assuming your app/intro_to_flask/__init__.py looks like this:

from flask import Flask
app = Flask(__name__)

import intro_to_flask.routes

Your app/runserver.py should look like this:

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

intro_to_flask is the imported module, app is a public object defined inside the imported module.

Cani
  • 1,129
  • 10
  • 14
  • Thank you Hendrik, You are right about my confusion. One problem is not really understanding app in the context of flask. Can you point me to some documentation that discusses this in more detail. I've read through http://flask.pocoo.org/docs/0.10/quickstart/ but could use another explanation – user1592380 Jan 29 '16 at 18:43
  • I've fixed a little mistake in my last example. Of course you have to import the instance of the Flask class which you have created in the `__init__.py` and then call `run` on the instance. Is it more clear now? – Cani Jan 29 '16 at 19:22