Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 尽管绝对进口,循环进口_Python 3.x_Python Import - Fatal编程技术网

Python 3.x 尽管绝对进口,循环进口

Python 3.x 尽管绝对进口,循环进口,python-3.x,python-import,Python 3.x,Python Import,我在Python3.9中遇到了循环导入的问题。 我知道有很多关于这个话题的文章、问题和答案,我真的读过很多,也读过很多关于如何规避的文章,但我不明白为什么会发生在我的案例中。据我所知,可以通过绝对进口绕过循环进口: 这是我的Stacktrace: partially initialized module 'myproject.myapp.service.linepattern.corner' has no attribute 'Corner' (most likely due to a circ

我在Python3.9中遇到了循环导入的问题。
我知道有很多关于这个话题的文章、问题和答案,我真的读过很多,也读过很多关于如何规避的文章,但我不明白为什么会发生在我的案例中。据我所知,可以通过绝对进口绕过循环进口:

这是我的Stacktrace:

partially initialized module 'myproject.myapp.service.linepattern.corner' has no attribute 'Corner' (most likely due to a circular import)

  File "/mnt/d/User/Programming/MyProject/myproject/myapp/service/linepattern/line.py", line 8, in Line
    def __init__(self, cornerRight: cc.Corner, cornerLeft: cc.Corner) -> None:
  
  File "/mnt/d/User/Programming/MyProject/myproject/myapp/service/linepattern/line.py", line 7, in <module>
    class Line(ABC):
  
  File "/mnt/d/User/Programming/MyProject/myproject/myapp/service/linepattern/corner.py", line 2, in <module>
    import myproject.myapp.service.linepattern.line as cl
  
  File "/mnt/d/User/Programming/MyProject/myproject/myapp/service/linepattern/cornermanager.py", line 2, in <module>
    import myproject.myapp.service.linepattern.corner as cc
  
  File "/mnt/d/User/Programming/MyProject/myproject/tests/myapp/test_cornermanager.py", line 3, in <module>
    import myproject.myapp.service.linepattern.cornermanager as cm
最后是我的代码

cornermanager.py

import myproject.myapp.service.linepattern.corner as cc

class CornerManager:
    def __init__(self) -> None:
        self.cornerList: List[cc.Corner]
        for i in range(1, 2):
            self.cornerList.append(cc.Corner(i, self))
        for corner in self.cornerList:
            corner.initializeLines()

    def corner(self, cornerNr: int) -> cc.Corner:
        return self.cornerList[cornerNr - 1]
corner.py

import myproject.myapp.service.linepattern.cornermanager as cm
import myproject.myapp.service.linepattern.line as cl

class Corner:
    def __init__(self, cornerNumber: int, cornerManager: cm.CornerManager) -> None:
        self.cornerNumber = cornerNumber
        self.cornerManager = cornerManager
        self.lineSet: Set[cl.Line]
        
    def initializeLines(self) -> None:
        #Add lines to the corner including its neighbor
        if self.cornerNumber == 1:
            self.lineSet.add(cl.Line(self, self.cornerManager.corner(2)))
        elif self.cornerNumber == 2:
            self.lineSet.add(cl.Line(self, self.cornerManager.corner(2)))

    
    def addLine(self, line: cl.Line) -> None:
        #Remove line from both corners
        for corner in cl.cornerPair():
            corner.lineSet.add(line)
        
    def removeLine(self, line: cl.Line) -> None:
        #Remove line from both corners
        for corner in cl.cornerPair():
            corner.lineSet.remove(line)
line.py

import myproject.myapp.service.linepattern.corner as cc

class Line:
    def __init__(self, cornerRight: cc.Corner, cornerLeft: cc.Corner) -> None:
        self.rightCorner = cornerRight
        self.leftCorner = cornerLeft        
    
    def cornerPair(self) -> Tuple[cc.Corner, cc.Corner]:
        #Return both corners
        return (self.rightCorner, self.leftCorner)


使用您在问题中输入的代码(三个独立的模块),通过在每个文件的顶部包含以下代码,应该能够修复问题:

from __future__ import annotations
简而言之,此代码将注释视为
字符串
。您可以在以下网址了解更多信息:和

对于错误,这实际上是Python执行模块的方式(从
cornermanager.py开始)

  • 导入角
    。在运行导入的模块代码之前,Python首先将模块保存在其内部模块(
    sys.modules
    )中
  • corner.py
    本身中,前两行是
    import语句
    ,因此让我们从
    import cornermanager
    开始。同样,Python将模块保存在其内部模块中,然后执行
    cornermanager
    中的代码
  • 返回到
    cornermanager.py
    。正如我们所知,这个模块的第一行是
    import corner
    ,但是由于Python已经在其内部模块中保存了这个模块(
    corner
    ),所以它不会第二次导入它。也就是说,Python跳过它并在这一行下面执行代码。有趣的是,由于
    corner
    除了第一行(
    import cornermanager
    )之外,没有执行所有代码,因此尝试获取其
    corner类
    将不会成功

  • “据我所知,可以通过绝对导入绕过循环导入:“遗憾的是,没有。循环导入是模块之间的依赖性问题。如何识别这些模块无关紧要。
    from __future__ import annotations