Import issue with __init__.py file Python: loading one subpackage automatically imports all the other subpackages -
i have python package structure:
package/ __init__.py suba/ __init__.py a.py subb/ __init__.py b.py in package/__init__.py have added 2 statements:
import suba import subb what want achieve able import whole package using import package , able use package.auba , package.subb.
it works, can't load package.suba. if use import package.suba package.subb imported. if use import package.suba sa sa available.
is normal behaviour, or doing wrong? how can chose import statements using import package or individual subpackages without loading others?
this normal behaviour. since have
import suba import subb if add few print statements in __init__.py files understand better. when import import package.suba, 'package' gets imported.
this package's __init__ file
this suba's __init__ file
this subb's __init__ file
doing print dir() add more clarity.
when import package.suba
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'package']
when import package.suba sa
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'sa']
Comments
Post a Comment