2

I'm working with two files: construct\core.py and USBtransport.py. Below is the relevant code from both files:

construct\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
....

where Subconstruct is a subclass of Construct which were both defined earlier in the code.

USBtransport.py:

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

I run into this error when trying to run USBtransport.py:

ImportError: cannot import name 'Range'

I've looked at similar posts about this error and they seem to all stem from circular importing. However I don't think that's the issue here since core.py never calls any classes from USBtransport.py. I'm also able to import other Subconstruct objects from core.py without problems.

I also don't think the error stems from the code inside class Range(Subconstruct) since I get the same error when I comment out all the code inside and just try to import an empty class.

Any ideas?

jackl
  • 21
  • 3

1 Answers1

0

Are you sure you're using the right version of Construct?

According to https://construct.readthedocs.io/en/latest/transition29.html , Range was removed in 2.9, so you might want to install 2.8.x.

Range removed, GreedyRange does not support [:] syntax

AKX
  • 152,115
  • 15
  • 115
  • 172
  • I'm using Construct 2.8.22, which has Range defined in it – jackl May 30 '18 at 13:34
  • If it helps, the full code for USBtransport is here: https://github.com/Parrot-Developers/sequoia-ptpy/blob/master/ptpy/transports/usb.py – jackl May 30 '18 at 13:37
  • Ah, you should have mentioned `core.py` is not your code either, but `construct/core.py`. – AKX May 30 '18 at 13:39
  • 1
    Anyway, without further information about what you're doing and what exactly is importing these things, I'm not sure I can decipher this situation much more. :( – AKX May 30 '18 at 13:40