简体   繁体   中英

How to import partial attributes/variables without namespace of submodules in python?

package tree

.
└── a
    ├── b
    │   ├── fun.py
    │   └── __init__.py
    └── __init__.py

content of a/__init__.py

from .b import  fun

content of a/b/__init__.py

from .fun import  fun

content of a/b/fun.py

def fun:
    print(111)

I just run ipython , and run

In [1]: import a                                                                                                                                                                                              

In [2]: dir(a)                                                                                                                                                                                                
Out[2]: 
['__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__path__',
 '__spec__',
 'b',
 'fun']

In [3]:     

issue

The issue is i don't want 'b' in a's dir. I expect the package just export 'fun'.

modify a/__init__.py as below:

from .b import  fun
del(b)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM