Below is the structure of my package:
my_package/
├── __init__.py
├── mod1.py
├── mod2.py
└── mod3.py
Below is the sample code of my package:
$ cat my_package/__init__.py
__all__ = ['mod1', 'mod2']
$ cat my_package/mod1.py
print("this is mod1")
class Mod1():
def do1(self):
print("do 1")
$ cat my_package/mod2.py
print("this is mod2")
class Mod2():
def do2(self):
print("do 2")
$ cat my_package/mod3.py
print("this is mod3")
class Mod3():
def do3(self):
print("do 3")
An error is reported when importing in the following way: AttributeError: 'module' object has no attribute 'mod1'
>>> import my_package
>>> my_do = my_package.mod1.Mod1()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'mod1'
>>>
I changed __init__.py to the following:
__all__ = ['mod1', 'mod2']
from my_package import *
No error was reported when importing in the following way:
>>> import my_package
this is mod1
this is mod2
>>> my_do = my_package.mod1.Mod1()
>>> my_do.do1()
do 1
>>>
So I think __all__ does not work, am I right? If not, what is the root case?