python from ... import ...起作用,但是import ...引发TypeError

时间:2018-12-04 20:06:06

标签: python-3.x

由于某些原因,当我调用k8s.projects_v2.Projects()时,导入k8s.projects_v2无法正常工作,但是当我调用Projects时,k8s.projects_v2导入项目却可以正常工作。

我还有另一个导入k8s.projects_v2的类,它正在工作。所有类都在同一包中。

有人可以指出我为什么会出现TypeError的方向吗?PyCharm对此py文件没有显示任何错误/警告。

虽然它以“从/导入”方式工作,但我想了解为什么在一个导入中而不是在另一个导入中工作!

我运行以下代码段;

import logging
import k8s.instance
import k8s.projects_v2
from typing import Union

CELL_KEY: str = 'cell'

CELLS_KEY: str = 'cells'

EXTENDS_KEY: str = 'extends'


_logging = logging.getLogger(__name__)


class Cell(k8s.instance.Instance):

    def __init__(self):
        super().__init__()

        self.__projects = k8s.projects_v2.Projects()

我明白了

DEBUG:__main__:Infrastructure.load_from_yaml(<class 'str'>)
Traceback (most recent call last):
  File "C:/Users/Dick/PycharmProjects/koolie/python/koolie/k8s/infrastructure_v2.py", line 105, in <module>
    infrastructure.load_from_yaml('infrastructure_v2.yaml')
  File "C:/Users/Dick/PycharmProjects/koolie/python/koolie/k8s/infrastructure_v2.py", line 75, in load_from_yaml
    self.load(yaml.load(file))
  File "C:/Users/Dick/PycharmProjects/koolie/python/koolie/k8s/infrastructure_v2.py", line 65, in load
    self.cells.load(data)
  File "C:\Users\Dick\PycharmProjects\koolie\python\koolie\k8s\cells_v2.py", line 58, in load
    cell: Cell = Cell()
  File "C:\Users\Dick\PycharmProjects\koolie\python\koolie\k8s\cells_v2.py", line 23, in __init__
    self.__projects = k8s.projects_v2.Projects()
TypeError: 'Projects' object is not callable

如果我跑步,那么

import logging
import k8s.instance
from k8s.projects_v2 import Projects
from typing import Union

CELL_KEY: str = 'cell'

CELLS_KEY: str = 'cells'

EXTENDS_KEY: str = 'extends'


_logging = logging.getLogger(__name__)


class Cell(k8s.instance.Instance):

    def __init__(self):
        super().__init__()

        self.__projects = Projects()

我没有错误!

1 个答案:

答案 0 :(得分:0)

我发现了问题!在Infrastructure.py中,我有以下错别字,最后一行是复合作业。第一个'='应该是':'。结果是我正在将新的Projects()对象分配给Projects类指针...

def __init__(self):
    super().__init__()
    self.cells = k8s.cells_v2.Cells()
    self.clusters: k8s.clusters.Clusters = k8s.clusters.Clusters()
    self.tokens: k8s.tokens_v2.Tokens = k8s.tokens_v2.Tokens()
    self.projects = k8s.projects_v2.Projects = k8s.projects_v2.Projects()