Why is a subpackage imported implicitly in Python?

I have a python package named a with two files in it __init__.py and b .

 a/ a/__init__.py a/b.py 

File Contents:

 # a/__init__.py from .b import * 

and

 # a/b.py c = 1 

When I import a into Python, why can I use ab directly? I am confused because I did not import b explicitly into a/__init__.py .

If I use empty a/__init__.py , then there is no b in the namespace a . So it seems that from .b import * (or from .b import c ) also imports b , why?

I checked the Python document and a few posts in SO, but did not find any related problems.


I want all things from b , but not b myself in a , then I could change a/__init__.py to

 # a/__init__.py from .b import * del b 

It's good?

+5
source share

All Articles