0

I'm get an ImportError based on which file I call the modules from. Here's the directory tree:

my_package/
     --base.py
     --derived.py
     --run1.py
run2.py

Note that run1.py is inside the my_package folder, while run2.py is outside the my_package folder.

# base.py
  class Base:
    ...

To avoid the ImportError in run1.py, I need the following import statement in derived.py:

# derived.py
from base import Base
class Derived(Base):
   ...

To avoid the ImportError in run2.py, I need the following import statement in derived.py:

# derived.py
from . import base
class Derived(base.Base):
   ...

Is there a way to import the Base class into the Derived class and have it work from both run1.py and run2.py?

Sergey
  • 5
  • 1
  • 3
  • 2
    similar to https://stackoverflow.com/questions/16981921/relative-imports-in-python-3 – Sach Nov 30 '20 at 03:25
  • change into it `from .base import Base` and in run2.py `from my_package.base import Base` – Adam Strauss Nov 30 '20 at 06:28
  • @AdamStrauss, what you said works when running run2.py but not run1.py. Is there a "universal" import statement that would work when referencing the modules from any location in directory tree? – Sergey Nov 30 '20 at 20:18

0 Answers0