我在名为users
的软件包中包含以下文件:
文件__init__.py
:
from flask_sqlalchemy import SQLAlchemy
from .views import UserDetails, UserList
db = SQLAlchemy()
文件models.py
:
from users import db
class User(db.Model):
pass
和文件views.py
:
from .models import User
from users import db
#code
但是发生了以下导入异常:
Error: While importing "users", an ImportError was raised:
Traceback (most recent call last):
File "/var/www/microservices/venv/lib/python3.6/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "/var/www/microservices/Flask_Microservices/users/__init__.py", line 9, in <module>
from .views import UserDetails, UserList
File "/var/www/microservices/Flask_Microservices/users/views.py", line 5, in <module>
from .models import User
File "/var/www/microservices/Flask_Microservices/users/models.py", line 2, in <module>
from users import db
ImportError: cannot import name 'db'
关于我的进口商品有什么问题的任何想法吗?
答案 0 :(得分:4)
疯狂的猜测。尝试移动
from .views import UserDetails, UserList
在db = SQLAlchemy()
下,看起来像
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
from .views import UserDetails, UserList
从视图执行导入时,它会尝试从views.py文件中的 init .py导入数据库。它尚不存在,因此会发生错误。至少我是这样认为的。