我在工作区中有工作空间和任务蓝图,工作空间视图工作空间/ apis.py和任务视图任务/ apis.py
我想从workspace / apis.py中的Task模型导入Task类,但我不能。
工作区模型:
class Workspace(db.Model):
__tablename__ = 'workspace'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Unicode(50), nullable=False)
created_on = db.Column(db.DateTime, default=datetime.utcnow)
users = db.relationship('User', secondary=workspace_users,
backref=db.backref('workspaces', lazy='dynamic'), lazy='dynamic')
owners = db.relationship('User', secondary=workspace_owners,
backref=db.backref('own_workspaces', lazy='dynamic'), lazy='dynamic')
teams = db.relationship('Team', cascade="all,delete", backref='workspace', lazy='dynamic')
projects = db.relationship('Project', cascade="all,delete", backref='workspace', lazy='dynamic')
tags = db.relationship('Tag', cascade="all,delete", backref='workspace', lazy='dynamic')
user_costs = db.relationship('UserRate', backref='workspace', lazy='dynamic')
clients = db.relationship('Client', backref='workspace', lazy='dynamic')
和任务模型:
class Task(db.Model):
__tablename__ = 'task'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Unicode(120), nullable=False)
start_time = db.Column(db.DateTime, default=datetime.datetime.utcnow)
end_time = db.Column(db.DateTime)
second = db.Column(db.Integer)
rate = db.Column(db.Float)
status = db.Column(db.Boolean, default=True)
project_id = db.Column(db.Integer, db.ForeignKey('project.id'))
owner_id = db.Column(db.Integer, db.ForeignKey('user.id'))
tags = db.relationship('Tag', secondary=task_tag,
backref=db.backref('tasks', lazy='dynamic'), lazy='dynamic')
在任务视图中我可以导入Workspace类,但在工作区视图中我无法导入任务类。
错误:
Traceback (most recent call last):
File "./run.py", line 8, in <module>
from project.application import create_app, db
File "/home/itmard/git/toggle-dev/project/__init__.py", line 1, in <module>
from project.application import create_app
File "/home/itmard/git/toggle-dev/project/application.py", line 5, in <module>
from project.apps.task.models import calculate_user_rate
File "/home/itmard/git/toggle-dev/project/apps/task/__init__.py", line 12, in <module>
from . import apis
File "/home/itmard/git/toggle-dev/project/apps/task/apis.py", line 12, in <module>
from project.apps.task.models import Task
File "/home/itmard/git/toggle-dev/project/apps/task/models.py", line 7, in <module>
from project.apps.user_rate.models import UserRate
File "/home/itmard/git/toggle-dev/project/apps/user_rate/__init__.py", line 8, in <module>
from . import views
File "/home/itmard/git/toggle-dev/project/apps/user_rate/views.py", line 10, in <module>
from project.apps.user_rate.forms import AddUserRateForm
File "/home/itmard/git/toggle-dev/project/apps/user_rate/forms.py", line 11, in <module>
from project.apps.workspace.models import workspaces
File "/home/itmard/git/toggle-dev/project/apps/workspace/__init__.py", line 12, in <module>
from . import apis
File "/home/itmard/git/toggle-dev/project/apps/workspace/apis.py", line 12, in <module>
from project.apps.task.models import Task
ImportError: cannot import name Task
项目结构:
├── project
│ ├── application.py
│ ├── apps
│ │ ├── task
│ │ │ ├── apis.py
│ │ │ ├── forms.py
│ │ │ ├── __init__.py
│ │ │ ├── __init__.py~
│ │ │ ├── models.py
│ │ │ └── views.py
│ │ └── workspace
│ │ ├── apis.py
│ │ ├── forms.py
│ │ ├── __init__.py
│ │ ├── models.py
│ │ └── views.py
答案 0 :(得分:3)
考虑以下情况:
main.py
from a import whats_up
print whats_up()
模块a:
first = 1
from b import second
third = 3
def whats_up():
return first + second + third
模块b:
second = 2
from a import first
print first
from a import third
print third
此代码遇到同样的问题。
模块a
在导入时会为其创建一个对象。然后,它将first
分配给该对象。接下来,Python将开始导入模块b
。请注意,我们尚未向模块third
分配a
!
加载模块b
后,它会分配second
,然后尝试从first
导入a
。这样成功,因此将打印1
。然后,它会尝试从third
导入a
。这是发生错误的地方:a
从未将其加载到定义third
的位置。
现在让我们看看你的堆栈跟踪:
您的追溯如下所示:
Traceback (most recent call last):
File "./run.py", line 8, in <module>
from project.application import create_app, db
File "/home/itmard/git/toggle-dev/project/__init__.py", line 1, in <module>
from project.application import create_app
File "/home/itmard/git/toggle-dev/project/application.py", line 5, in <module>
from project.apps.task.models import calculate_user_rate
File "/home/itmard/git/toggle-dev/project/apps/task/__init__.py", line 12, in <module>
from . import apis
File "/home/itmard/git/toggle-dev/project/apps/task/apis.py", line 12, in <module>
from project.apps.task.models import Task
...
File "/home/itmard/git/toggle-dev/project/apps/user_rate/forms.py", line 11, in <module>
from project.apps.workspace.models import workspaces
File "/home/itmard/git/toggle-dev/project/apps/workspace/__init__.py", line 12, in <module>
from . import apis
File "/home/itmard/git/toggle-dev/project/apps/workspace/apis.py", line 12, in <module>
from project.apps.task.models import Task
请注意...
之前的部分:此时,正在导入tasks.models
模块。但是,在第7行,您可以这样做:
from project.apps.user_rate.models import UserRate
在完全定义Task
模型之前,您开始加载user_rate.models
模块,该模块本身有一堆定义,如果您遵循堆栈跟踪,则返回其中一个模块还试图导入apps.tasks.models
。
这导致循环依赖性(例如,模块1依赖于模块2,模块2依赖于取决于模块1的模块3)。您需要重新思考模块如何相互依赖以解决此问题。