how to use other's code as a module in my project?

Viewed 49

File structure is:

├── module
│   ├── script1.py
│   ├── enums.py
├── script2.py

enums.py:

from enum import Enum, auto
        
class Color(Enum):
    RED = auto()
    BLUE = auto()

script1.py:

from enums import Color

blue = Color.BLUE

script2.py:


from module.enums import Color
from module.script1 import blue

assert blue.name == Color.BLUE.name
assert blue.value == Color.BLUE.value
assert blue == Color.BLUE

Run script2.py, get exception:

Traceback (most recent call last):
  File "/Users/python/py_helloworld/script2.py", line 2, in <module>
    from module.script1 import blue
  File "/Users/python/py_helloworld/module/script1.py", line 1, in <module>
    from enums import Color
ModuleNotFoundError: No module named 'enums'

What if module is written by others and I just want to use it in my project by copy-pasting it into my project folder without changing his code. When he wrote the module, since his pythonPath contains module folder so from enums import Color works. But what can I do instead of sys.path.append the module folder?

PS:

  • I can't sys.path.append('./module') because assert blue == Color.BLUE will be false
0 Answers
Related