I'm confused when I tried write myself __init__.py after saw the code in numpy\__init__.py library.
Here is numpy\__init__.py code snippet
__all__.extend(['__version__', 'pkgload', 'PackageLoader',
'show_config'])
__all__.extend(core.__all__)
__all__.extend(_mat.__all__)
__all__.extend(lib.__all__)
__all__.extend(['linalg', 'fft', 'random', 'ctypeslib', 'ma'])
And My directory structure is:
app/
......test.py
......lib1\
............ __init__.py
............ Lib1File.py
............ sublib1\
............ ............ __init__.py
............ ............ SubLib1File.py
The code in test.py is
from lib1 import *
if __name__ == "__main__":
result1 = Lib1File.add(10, 15) # a simple function in Lib1File.py
print(result1)
result2 = Sublib1File.mul(10,15) # a simple function in Sublib1File.py
print(result2)
The code in lib1\__init__.py is
from . import sublib1
__all__ = ["Lib1File"]
__all__.extend(sublib1.__all__)
print(__all__) # it can print ['Lib1File', 'Sublib1File'] on console
The code in lib1\sublib1\__init__.py is
__all__ = ["Sublib1File"]
But when I ran test.py, I got a error
*File "test.py", line 1, in module from lib1 import . AttributeError: module 'lib1' has no attribute 'Sublib1File'
My questions are
Why I get this error even if
__all__ = ['Lib1File', 'Sublib1File']
in lib1\__init__.py?How should I fix it if I still just use one import
from lib1 import *
?If we cannot solve question 2, what's the purpose of
__all__.extend(...)
in numpy\__init__?