python:ImportError:从文件一级向上导入类时无法导入名称“ X”

时间:2018-09-10 12:10:21

标签: python-3.x python-import

我的python项目中具有以下结构:

src
|_usr
  |_api
  | |_db
  |   |_ __init__.py (lets call it db/__init__.py_)
  |   |_answers.py
  |_ __init__.py (lets call it api/__init__.py)
  |_schema1.py

在schema1.py中,我定义了一个类Answer,该类需要导入db / answers.py文件中。 因此answers.py文件的开头是:

"""put and get from dynamoDB answers functions."""

import boto3
from .. import Answers

def getAnswers(userId: str, version: int) -> Answers:

然后在api/__init__.py文件中:

from .core import app
from .schema1 import Answers

__all__ = ['app', 'Answers']

最后在schema1.py文件中,有 Answer 类声明:

from typing import Dict, List, NamedTuple, Union

import graphene

from .db import get_questionnaire_in_dict, getAnswers, putAnswers
class Answer(graphene.ObjectType):  # type: ignore
    """Single question object, consists of question ID and selected answers."""

每当我运行代码时,都会出错:

     __import__(module)
   File "/project-root/src/user/api/__init__.py", line 2, in <module>
     from .core import app
   File "/project-root/src/user/api/core.py", line 6, in <module>
     from .schema1 import schema1
   File "/project-root/src/user/api/schema1.py", line 8, in <module>
     from .db import get_questionnaire_in_dict, getAnswers, putAnswers
   File "/project-root/src/user/api/db/__init__.py", line 2, in <module>
     from .answers import getAnswers, putAnswers
   File "/project-root/src/user/api/db/answers.py", line 6, in <module>
     from .. import Answers
 ImportError: cannot import name 'Answers'

问题是为什么类 Answers answers.py文件中不可见? 我在这里阅读了很多有关导入的类似主题,但是没有一种解决方案/建议指导我解决这种特殊情况。

0 个答案:

没有答案