[英]ImportError when importing from a lower module
I'm trying to import an object named db
(SQLAlchemy object) in in a module of my Flask Project in order to use the SQLAlchemy in my models ( models.py
). 我正在尝试在我的Flask项目的模块中导入名为
db
(SQLAlchemy对象)的对象,以便在我的模型中使用SQLAlchemy( models.py
)。 Assuming my package is named Foo
and contains the db
object in his __init__.py
file , when i try to do a from Foo import db
, i get the following error: 假设我的包被命名为
Foo
并且在他的__init__.py
文件中包含db
对象,当我尝试from Foo import db
执行一个时,我收到以下错误:
ImportError: cannot import name db
I'm using Flask Blueprint to dispatch my project into two applications (dashboard and frontend) and each of them contains an __init__.py
file. 我正在使用Flask Blueprint将我的项目分派到两个应用程序(仪表板和前端),每个应用程序都包含一个
__init__.py
文件。 Only the models.py
is throwing this error, i got some imports in my views file (as importing the login manager) and everything goes well. 只有
models.py
抛出这个错误,我在我的视图文件中有一些导入(导入登录管理器),一切顺利。
Any idea of what it could be ? 知道它可能是什么?
UPDATE : The traceback 更新:追溯
Traceback (most recent call last):
File "run.py", line 4, in <module>
from kuchiyose import app
File "/path_to_project/kuchiyose/kuchiyose/__init__.py", line 60, in <module>
from kuchiyose import dashboard, frontend
File "/path_to_project/kuchiyose/kuchiyose/dashboard/__init__.py", line 10, in <module>
from dashboard import views
File "/path_to_project/kuchiyose/kuchiyose/dashboard/__init__.py", line 10, in <module>
from dashboard import views
File "/path_to_project/kuchiyose/kuchiyose/dashboard/views.py", line 8, in <module>
from kuchiyose.dashboard.models import User
File "/path_to_project/kuchiyose/kuchiyose/dashboard/models.py", line 3, in <module>
from kuchiyose import db
ImportError: cannot import name db
First, thanks for the help. 首先,感谢您的帮助。 Concerning my second question : "How to import Flask models into views without having a circular import issue (when using SQLAlchemy)", i found a solution.
关于我的第二个问题:“如何在没有循环导入问题的情况下将Flask模型导入视图(使用SQLAlchemy时)”,我找到了一个解决方案。 It consists to setup the SQLAlchemy object not into the application
__init__.py
file but into the models.py
file itself. 它包括将SQLAlchemy对象设置为不在应用程序
__init__.py
文件中,而是设置在models.py
文件本身中。 With that, you can import it in your views.py
file without problem. 有了它,您可以在
views.py
文件中导入它没有问题。
My models.py file, now: 我的models.py文件,现在:
from kuchiyose import app
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)
And my views.py 还有我的views.py
from kuchiyose.dashboard import models
This is an old one but showed up as a top result in my search. 这是一个旧的,但在我的搜索中显示为最佳结果。
I have a project that has model declarations in a few different places rather than a consolidated models.py file so this solution wouldn't be ideal. 我有一个项目在几个不同的地方有模型声明而不是统一的models.py文件,所以这个解决方案不是理想的。 I found importing your views AFTER establishing the db object works as well.
我发现在建立db对象后也可以导入你的视图。
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)
# Now Register routes
# db is now defined so app import db will now work)
from routes.user import user as user_routes
app.register_blueprint(user_routes)
there appears to be circular imports: 似乎是循环进口:
kuchiyose/__init__.py
imports from kuchiyose.dashboard.views
kuchiyose/__init__.py
从kuchiyose.dashboard.views
kuchiyose/dashboard/views.py
imports from kuchiyose.dashboard.models
kuchiyose/dashboard/views.py
从kuchiyose.dashboard.models
kuchiyose/dashboard/models.py
imports from kuchiyose
kuchiyose/dashboard/models.py
从kuchiyose
进口kuchiyose
see eg Circular (or cyclic) imports in Python for more info... 请参阅Python中的循环(或循环)导入以获取更多信息...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.