简体   繁体   English

Flask导入错误

[英]Flask import error

I have workspace and task blueprint in flask with workspace view workspace/apis.py and task view task/apis.py 我在工作区中有工作空间和任务蓝图,工作空间视图工作空间/ apis.py和任务视图任务/ apis.py

I want to import Task class from Task model in workspace/apis.py but i can't. 我想从workspace / apis.py中的Task模型导入Task类,但我不能。

workspace model: 工作区模型:

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')

and task model: 和任务模型:

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')

in task view i can import Workspace class but on workspace view i can't import Task class. 在任务视图中我可以导入W​​orkspace类但在工作区视图上我无法导入Task类。

error: 错误:

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 structure : 项目结构:

├── 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

Think about the following scenario: 考虑以下情况:

main.py main.py

from a import whats_up
print whats_up()

Module a: 模块a:

first = 1
from b import second
third = 3
def whats_up():
    return first + second + third

Module b: 模块b:

second = 2
from a import first
print first
from a import third
print third

This code suffers the same problem. 此代码遇到同样的问题。

Module a will, when being imported, have an object created for it. 模块a将在导入时为其创建一个对象。 It will then have first assigned to that object. 然后它将first分配给该对象。 Next, Python will start importing module b . 接下来,Python将开始导入模块b Note that we have not assigned third to module a yet! 请注意,我们还没有为模块a分配third

When module b is loaded, it will assign second , then attempt to import first from a . 加载模块b ,它将分配second ,然后尝试firsta导入。 This succeeds, and so 1 will be printed. 这成功了,因此将打印1 Then it will attempt to import third from a . 然后它将尝试从a导入third a This is where the error occurs: a never finished its load to the point where it defined third . 这是错误发生时: a从未完成其负载到这种地步它定义third

Now let's look at your stack trace: 现在让我们来看看你的堆栈跟踪:

Your traceback looks like this: 你的追溯看起来像这样:

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

Note the section right before the ... : At this point, the tasks.models module is being imported. 请注意...之前的部分:此时,正在导入tasks.models模块。 However, on line 7 of that, you then do this: 但是,在第7行,您可以这样做:

from project.apps.user_rate.models import UserRate

Before the Task model could fully be defined, you start loading the user_rate.models module, which itself has a bunch of definitions which, if you follow the stack trace, lead back to one of those modules also trying to import apps.tasks.models . 在完全定义Task模型之前,您开始加载user_rate.models模块,该模块本身有一堆定义,如果您遵循堆栈跟踪,则返回其中一个模块,同时尝试导入apps.tasks.models

This is causing a circular dependency (eg, module 1 depends on module 2 which depends on module 3 which depends on module 1). 这导致循环依赖性(例如,模块1取决于模块2,模块2取决于取决于模块1的模块3)。 You need to rethink how your modules depend on each other to solve this problem. 您需要重新思考模块如何相互依赖以解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM