1

Im trying to import my app config (db_config.py) via:

>>> from app.db_config import db_session

Error received 'ImportError: No module named 'app.db_config'; 'app' is not a package

app.py looks like:

import pymysql.cursors
from flask import Flask, render_template, request
from flask_migrate import Migrate
from app.db_config import db_session

#app setup
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:mypass@localhost/mydb'
db = SQLAlchemy(app)
migrate = Migrate(app, db)
@app.teardown_appcontext
def shutdown_session(exception=None):
    db_session.remove()

@app.route('/', methods=('GET', 'POST'))
def index():
    return 'meow'
if __name__ == '__main__':
    app.run()

db_config.py:

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('mysql+pymysql://root:mypass@localhost/mydb',
                                         convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
                                         autoflush=False,
                                         bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()

def init_db():
    import app.db_table
    Base.metadata.create_all(bind=engine)

db_table.py:

from sqlalchemy import Column, Integer, String, DateTime, Boolean
from sqlalchemy.ext.declarative import declarative_base
from app.db_config import Base

Base = declarative_base()

class Tld(Base):
    __tablename__ = 'Tld'
    id = Column(Integer, primary_key=True)
    uri =  Column(String(80), unique=True)
    tmstmp = Column(DateTime())
    auth = Column(Boolean())

    def __init__(self, uri=None):
        self.uri = uri
        self.tmstmp = tmstmp
        self.auth = auth

What am I doing wrong here? I'm following this tutorial but Im not receiving the output i expected.

Is something wrong with my app.py that makes it not callable like that? or something else wrong with it?

(First time alchemy user)

Thank you so much

1
  • I take it your package is also named app. So you got a naming conflict here. As you module is named app.py Python tries to import db_config from your module. Try renaming your module (I use __init__.py). Commented Jan 3, 2017 at 15:28

1 Answer 1

1

I resolved this myself. I needed to do from db_config import db_session, instead of from app.db_config import db_session

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.