Import issues with flask app - python

I had the following structure in my flask app:
app.py:
app = Flask(__name__)
app.config.from_object(os.environ['APP_SETTINGS'])
db = SQLAlchemy(app)
from models import *
models.py:
from app import db
It worked fine until I wanted to do read/write operations on models in files other than app.py. I tried to import model Trackorder in a file tasks.py but got the following error:
ImportError: cannot import name TrackOrder
So, I changed the structure:
__init__.py:
app = Flask(__name__)
app.config.from_object(os.environ['APP_SETTINGS'])
db = SQLAlchemy(app)
But this makes app and db unavailable in app.py and models.py:
File "app.py", line 21, in <module>
from models import *
File "/home/nish/repos/stage/voylla_api/models.py", line 16, in <module>
class Product(db.Model):
NameError: name 'db' is not defined
##after commenting models.py:
Traceback (most recent call last):
File "app.py", line 210, in <module>
#app.route('/')
NameError: name 'app' is not defined
How can I resolve this issue?

Here is a solution that could work for you.
Create a file named core.py (or whatever you want to name it):
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
Now in app.py:
from core import db
app = Flask(__name__)
app.config.from_object(os.environ['APP_SETTINGS'])
# Instead of this line: db = SQLAlchemy(app)
# Use this approach to initialize db
db.init_app(app)
In your models.py you can use this import from core import db
This is based on the example here: https://pythonhosted.org/Flask-SQLAlchemy/api.html

Without the full code with all import statement it's hard to say, but you most likely have a cyclic import. You are importing module B in module A and module B in module A.
This has the side-effect, that at the time you are importing A into B (the import that closes the cycle) nothing below the import of B is available in A. An example:
a.py:
from b import * # Now b.py is evaluated before the execution contiues.
var1 = 0
b.py:
import a
print(a.var1) # This raises an error since var1=0 was not executed yet.
Solution:
Reorder your imports or use the import statement locally, like:
def function1():
from a import var1
print(var1)

Related

my test python flask app is returning this "partially initialized module 'project' has no attribute 'db' (most likely due to a circular import)"

this is my project directory structure shown in the images with the links below, from my own understanding l am creating a db object in the project directory's init.py file, that object is being imported by models.py please help me understand the nature of the circular import problem and how l can fix it
Folder Structure Images:
Folder Structure
Project Structure
#traceback
PS Projects\test_app> py .\runServer.pyTraceback (most recent call last):File "C:\Users\Projects\test_app\runServer.py",
line 1, in \<module\>from project import app
File "C:\\Users\\Projects\\test_app\\project_init\_.py",
line 4, in \<module\>from .routes import routes
File "C:\\Users\\Projects\\test_app\\project\\routes.py",
line 3, in \<module\>from project import models
File "C:\\Users\\Projects\\test_app\\project\\models.py",
line 2, in \<module\>from project import db
ImportError: cannot import name 'db' from partially initialized module 'project' (most likely due to a circular import)
`
#models-module
from project import db
import uuid
class User(db.Model):
id = db.Column(db.String(455), primary_key=True, default=str(uuid.uuid4()))
name = db.Column(db.String(30))
surname = db.Column(db.String(30))
#routes-module
from project import models
from flask import Blueprint
routes = Blueprint("routes",__name__)
#routes.route("/")
def index():
user = models.User(name="Nyasha",surname="Chiza")
models.db.session.add(user)
models.db.session.commit()
return "Landing Page"
#routes.route("/home")
def home():
user = models.User.query.first()
return 'Welcome {0} {1}'.format(user.name,user.surname)`
#project/__init__.py
from flask import Flask, Blueprint
from flask_sqlalchemy import SQLAlchemy
from .routes import routes
import os
app = Flask(__name__)
db = SQLAlchemy(app)
app.register_blueprint(routes)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///database.db"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY")`

ImportError: cannot import name 'db' from [package_name] [duplicate]

This question already has an answer here:
What is the correct way to solve this circular import error with a Flask blueprint?
(1 answer)
Closed 3 years ago.
I have a complicated code structure. The Project folder has a mother package that holds two more child package. I need to import a SQLAlchemy instance, db from the the mother package's init.py file to a child packages db_models.py file. the db_models.py holds all the routes that exchanges calls to the database. I have seen other solutions that came up but those didn't solve the problem as mine has multiple packages. Following is the hierarchy of my project.
FlaskUserAuthentication(project)
FlaskUserAuthentication(package)=>
API(package)=>
__init__.py
db_models.py
routes.py(Blueprint)
Site(package)=>
__init__.py
routes.py(Blueprint)
templates=>
index.html
signin.html
signup.html
__init__.py
run.py (under project)
Under the project folder FlaskUserAuthentication, I have a package with the same name(FlaskUserAuthentication). The following is the code from its __init__.py file.
from flask import Flask
from FlaskUserAuthentication.API.routes import api
from FlaskUserAuthentication.Site.routes import site
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
db = SQLAlchemy(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
app.register_blueprint(api)
app.register_blueprint(site)
Under the two child packages under namely API and Site, there are routes.py files one of which handles the database queries(API/routes.py) and another one renders the views(Site/routes.py). Both of these packages has a Blueprint instance.
The following is the code from API(package)/routes.py file-
from flask import jsonify, Blueprint, request
from FlaskUserAuthentication.API.db_models import Member
api = Blueprint('api', __name__)
#api.route('/members')
def get_all_members():
members = Member.query.all()
return jsonify({'response': 'This will return the members from the sqlite database'})
However at the second line, where I am trying to import the SQLAlchemy model class, Member, I am getting some error which doesn't refer to that line but it was okay before I added that line, so I realized this was it. The following is the error I am getting -
Traceback (most recent call last):
File "F:\FlaskUserAuthentication\run.py", line 1, in <module>
from FlaskUserAuthentication import app
File "F:\FlaskUserAuthentication\FlaskUserAuthentication\__init__.py", line 2, in <module>
from FlaskUserAuthentication.API.routes import api
File "F:\FlaskUserAuthentication\FlaskUserAuthentication\API\routes.py", line 2, in <module>
from FlaskUserAuthentication.API.db_models import Member
File "F:\FlaskUserAuthentication\FlaskUserAuthentication\API\db_models.py", line 1, in <module>
from FlaskUserAuthentication import db
ImportError: cannot import name 'db' from 'FlaskUserAuthentication' (F:\FlaskUserAuthentication\FlaskUserAuthentication\__init__.py)
The following code is from the API/db_model.py file-
from FlaskUserAuthentication import db
from datetime import datetime
class Member(db.Model):
id = db.Column('id', db.Integer, primary_key=True)
username = db.Column('user_name', db.String(100), nullable=False)
email = db.Column('email', db.String(100), nullable=False, unique=True)
password = db.Column('password', db.String(100), nullable=False, unique=False)
However, If I comment out the from FlaskUserAuthentication.API.db_models import Member, under the API/routes.py file, I don't get this error.
I realized it must be a circular import issue but can anyone please explain me how to avoid such error?
How can I import my db_model classes in my routes.py file?
It looks like you are importing db (in API/db_model.py) before it's initialized. The quick and dirty solution might be to import api after you initialize the db object in the top level __init__.py.
from flask import Flask
from FlaskUserAuthentication.Site.routes import site
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
db = SQLAlchemy(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
from FlaskUserAuthentication.API.routes import api
I suggest you use the app factory pattern, however, as your application grows in complexity.

Access db from a separate file flask SQLAlchemy python3

I am writing a flask application. I have two files. main.py and databases.py. I want to create a database from the database.py file. The main.py should access the databases.py file and create the database and table named "Users". But it shows import error. Help me with this issue
main.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from databases import User
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/data_log.db'
db = SQLAlchemy(app)
if __name__ == '__main__':
db.create_all()
app.run(host='0.0.0.0', port=5001)
databases.py
from main import db
from passlib.apps import custom_app_context as pwd_context
class User(db.Model) :
__tablename__ = 'users'
user_id = db.Column(db.Integer, primary_key = True)
username = db.Column(db.String(32), index = True)
password = db.Column(db.String(128))
def hash_password(self, password) :
self.password =pwd_context.hash(password)
def verify_password(self, password) :
return pwd_context.verify(password, self.password)
Traceback:
Traceback (most recent call last):
File "main.py", line 3, in <module>
from databases import User
File "/home/paulsteven/stack/databases.py", line 1, in <module>
from main import db
File "/home/paulsteven/stack/main.py", line 3, in <module>
from databases import User
ImportError: cannot import name 'User'
This is a regular case of cyclic imports conflict.
The traceback gives you a clear steps:
How it goes in steps:
you run main.py
the control flow starts importing features/libraries till it gets the 3rd line
from databases import User.
it goes to the databases module to find the needed User class. But ... the User may use the outer scope features (and it does require db.Model), so the control flow needs to scan databases module from the start. This reflects the 2nd step from traceback ("/home/paulsteven/stack/databases.py", line 1)
from the position of previous step being at database.py the control flow encounters from main import db - that means it should turn back to the main(main.py) module!
control flow returned to the main module start scanning again from the 1st line - till it finds from databases import User again. This reflects the traceback's 3rd step (File "/home/paulsteven/stack/main.py", line 3)
and you run into cycle ...
What is right way to solve the issue?
Keep all DB context/DB models in separate module(s).
Follow the sequence of objects relations and how they depend on each other:
---> Application instantiated first (app)
---> then DB framework instance is created db = SQLAlchemy(app) depending on app
---> then custom DB models created (like User(db.Model)) depending on db instance
main.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
# from databases import User <--- shouldn't be here
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/data_log.db'
db = SQLAlchemy(app)
if __name__ == '__main__':
from databases import User # after instantiating `db` import model(s)
db.create_all()
app.run(host='0.0.0.0', port=5001)

Flask circular dependency

I am developing a Flask application. It is still relatively small. I had only one app.py file, but because I needed to do database migrations, I divided it into 3 using this guide:
https://realpython.com/blog/python/flask-by-example-part-2-postgres-sqlalchemy-and-alembic/
However, I now can't run my application as there is a circular dependency between app and models.
app.py:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask import render_template, request, redirect, url_for
import os
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DB_URL']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.debug = True
db = SQLAlchemy(app)
from models import User
... routes ...
if __name__ == "__main__":
app.run()
models.py:
from app import db
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(120), unique=True)
def __init__(self, username, email):
self.username = username
self.email = email
def __repr__(self):
return self.username
manage.py:
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from app import app, db
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
if __name__ == "__main__":
manager.run()
They are all in the same directory. When I try to run python app.py to start the server, I receive an error which definitely shows a circular dependency (which is pretty obvious). Did I make any mistakes when following the guide or is the guide wrong? How can I refactor this to be correct?
Thanks a lot.
EDIT: Traceback
Traceback (most recent call last):
File "app.py", line 14, in <module>
from models import User
File "/../models.py", line 1, in <module>
from app import db
File "/../app.py", line 14, in <module>
from models import User
ImportError: cannot import name User
I propose the following structure:
# app/extensions.py
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
...
# app/app.py
from app.extensions import db
def create_app(config_object=ProdConfig):
app = Flask(__name__.split('.')[0])
app.config.from_object(config_object)
register_extensions(app)
...
def register_extensions(app):
db.init_app(app)
...
# manage.py
from yourapp.app import create_app
app = create_app()
app.debug = True
...
In this case, database, app, and your models are all in separate modules and there are no conflicting or circular imports.
I chased this for a few hours, landing here a few times, and it turned out I was importing my page modules (the ones holding the #app.route commands) before the line where the app was created. This is easy to do since import commands tend to be placed at the very beginning, but it doesn't work in this case.
So this:
# app/__init__.py
print("starting __init__.py")
from flask import Flask
from flask import render_template
import matplotlib.pyplot as plt
import numpy as np
import mpld3
app = Flask(__name__, instance_relative_config=True)
app.config.from_object('config')
from . import index
from . import simple
app.run(threaded=False)
print("finished __init__.py")
Instead of having all imports on top.
Placing this here because this has to be a common error for casual flask users to encounter and they are likely to land here. I have hit it as least twice in the last couple of years.

flask sqlalchemy example around existing database

Problem: there needs to be full working example of auto-mapping sqlalchemy to an existing database in an app with multiple binds.
I want to bind to two databases and have one auto-map the tables. I need to do this because i don't have control over one DB, thus would have to constantly re-write my models and might delete new tables every time i migrate.
I love the answers given here, but i can't seem to make it work in Flask (I am able to use the sqlalchemy only to query as per the example).
The model.py I set up from the example above results in
EDIT I pulled the line
db.Model.metadata.reflect[db.engine]
from another post, and it should be db.Model.metadata.reflect(db.engine)
very simple solution
here is my model.py
from app import db
from sqlalchemy.orm import relationship
db.Model.metadata.reflect[db.engine]#change to (db.engine)
class Buildings(db.Model):
__table__ = db.Model.metadata.tables['test']
__bind_key__ = 'chet'
def __repr__(self):
return self.test1
.... other models from sqlalchemy uri here...
i get this
>>> from app import db, models
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "app/__init__.py", line 69, in <module>
from app import views, models
File "app/views.py", line 1, in <module>
from app import app,models, db
File "app/models.py", line 163, in <module>
db.Model.metadata.reflect[db.engine]
TypeError: 'instancemethod' object has no attribute '__getitem__'
Here's my config.py
SQLALCHEMY_DATABASE_URI = 'postgresql://chet#localhost/ubuntuweb'
SQLALCHEMY_BINDS = {
'chet': 'postgresql://chet#localhost/warehouse',
}
here's my init.py file
from flask import Flask
from flask_bootstrap import Bootstrap
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.admin import Admin, BaseView, expose
from flask.ext.script import Manager
from flask.ext.migrate import Migrate, MigrateCommand
from flask.ext.login import LoginManager, UserMixin, login_required
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'app/static'
app.config.from_object('config')
db = SQLAlchemy(app)
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
Bootstrap(app)
from app import views, models
admin = Admin(app)
Your code has
db.Model.metadata.reflect[db.engine]
When it should be
db.Model.metadata.reflect(db.engine) # parens not brackets
This should have been pretty obvious from the stack trace....

Categories

Resources