Sin5k4
Sin5k4

Reputation: 1626

Python Importing Modules

I have the following app structure. IDE works fine, resolves, but running some scripts gives me

  File "/home/sink/TARET/app/models.py", line 4, in <module>
    from app import db
ImportError: No module named app

error. I call the module as:

import datetime
from app import db


class Role(db.Model):
    __tablename__ = 'role'
    RoleID = db.Column(db.Integer, primary_key=True)
    Name = db.Column(db.String(80), unique=True)
    ModifiedDate = db.Column(db.DATETIME) and so on

What is the correct usage of modules in python? I have the following structure.

enter image description here

Ok edit:

db is defined in init.py as

app = Flask(__name__)
app.debug = True
app.config.from_object('config')
db = SQLAlchemy(app)

Upvotes: 0

Views: 94

Answers (1)

Esparta Palma
Esparta Palma

Reputation: 745

On the bottom of your app/__init__.py put this:

from app import models

Why?

From http://flask.pocoo.org/docs/patterns/packages/

Circular Imports

Every Python programmer hates them, and yet we just added some: circular imports (That’s when two modules depend on each other. In this case views.py depends on __init__.py). Be advised that this is a bad idea in general but here it is actually fine. The reason for this is that we are not actually using the views in __init__.py and just ensuring the module is imported and we are doing that at the bottom of the file.

Upvotes: 1

Related Questions