首页 > 解决方案 > python,Windows 7:ImportError:无法导入名称 X

问题描述

我正在处理两个文件:construct\core.pyUSBtransport.py. 以下是两个文件中的相关代码:

构造\core.py:

class Range(Subconstruct):  
    __slots__ = ["min", "max"]
    def __init__(self, min, max, subcon):
        super(Range, self).__init__(subcon)
        self.min = min
        self.max = max
....

其中 Subconstruct 是 Construct 的子类,它们都在代码的前面定义。

USB传输.py:

from construct import Subconstruct
from construct import (
Bytes, Container, Embedded, Enum, ExprAdapter, Int16ul, Int32ul, Pass, 
Struct, Range,
)

尝试运行 USBtransport.py 时遇到此错误:

ImportError: cannot import name 'Range'

我查看了有关此错误的类似帖子,它们似乎都源于循环导入。但是我认为这不是问题所在,因为core.py从不调用任何类USBtransport.py。我也可以毫无问题地导入其他Subconstruct对象。core.py

我也不认为错误源于类内的代码,Range(Subconstruct)因为当我注释掉里面的所有代码并尝试导入一个空类时,我得到了同样的错误。

有任何想法吗?

标签: pythonimporterrorconstruct

解决方案


您确定您使用的是正确版本的Construct吗?

根据https://construct.readthedocs.io/en/latest/transition29.htmlRange已在 2.9 中删除,因此您可能需要安装 2.8.x。

Range 被移除,GreedyRange 不支持 [:] 语法


推荐阅读