Hi I'm building my own package and I have a question on __all__.
Are there any neat way to define __all__, other than explicitly typing each and every function in the module?
I find it very tedious...
I'm trying to make some code which wraps on frequently used libraries such as numpy
, pytorch
, os
. The problem is, the libraries I used to create my modules also gets imported when I import my package.
I want to import every function / class that I defined, but I don't want the third-party libraries that I used in the process to get imported.
I use from .submodule import *
in my __init__.py
so that I can access my functions inside the submodule directly. (Just like we can access functions directly from the top package like np.sum()
, torch.sum()
)
My submodule has a lot of functions, and I want to import all of them to __init__.py
, except for the third-party packages that I used.
I see that __all__ defines what to import when from package import *
is called.
For example,
utils.py
__all__ = ['a']
def a():
pass
def b():
pass
__init__.py
from .utils import *
and
>>> import package
>>> package.a()
None
>>> package.b()
NameError: 'package.b' is not defined
What I want is something like
__all__ = Some_neat_fancy_method()
I tried locals() and dir(), but got lost along the way. Any suggestions?
__all__
if you want to include everything – Dour__all__
in__init__
file and define which packages you want to make avaiable – Hollishollister