0

My Project looks like this:

├─outer_module
│  │  __init__.py
│  │
│  └─inner_module
│          a.py
│          b.py
├─test.py

__init__.py:

from outer_module.inner_module import a
from outer_module.inner_module import b

a.py:

instance_a = 1

b.py:

instance_b = 1
print("instance_b created!")

test.py:

from outer_module.inner_module import a

I want to shorten import path in test.py, i.e. use from outer_module import a. That is not unusual when I turn my project into a release module. But using __init__.py, it will automatically invoke b.py and print instance_b created!. Seperating a.py and b.py from inner_module is not recommended because they are functionally similar. Other .py file may invoke b.py so b.py must appear in __init__.py Could anyone give some advice?

Blake
  • 1
  • Duplicate of https://stackoverflow.com/questions/4383571/importing-files-from-different-folder – Asaf Mar 02 '22 at 06:19

1 Answers1

0

In your __init__.py file try importing the a and b files individually and adding the two imports to your __all__ variable.

from outer_module.inner_module import a
from outer_module.inner_module import b 

__all__ = [
    'a',
    'b',
]

Now you can import a and b directly from outer_module.

from outermodule import a
import outermodule.b as b