I am wondering why the directory (subpackage) that holds submodules in a python package shows up as a symbol when the package is imported. For instance, if I have this package:
PyModTest/ Top-level package
__init__.py Initialize the package
Source/ Subpackage holding source files
__init__.py
WildMod.py Submodule containing a function: 'WildFunc'
where the top level __init__.py looks like this:
#!/usr/bin/env python
from Source.WildMod import WildFunc
and, for completeness’ sake, the lower level __init__.py looks like this:
#!/usr/bin/env python
__all__ = ["WildMod"]
OK, so now I open up the interpreter, import the module, and look at the symbols:
>>> import PyModTest
>>> dir(PyModTest)
['Source', 'WildFunc', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__']
SEE, the ‘Source’ module shows up, even though I never specifically imported it!
The only symbol (besides the private ones) that I want to see is my ‘WildFunc’. Is there any way to hide the ‘Source’ package?
Two things to note here:
Sourceis actuallyPyModTest.Source(thanks to TokenMacGuy for pointing this out)So: in order to import
PyModTest.Source.WildMod.WildFunc, Python has toPyModTest(which was already done by you)Source, and if not, create the attribute by importing it fromPyModTest/Source/__init__.pyWildMod, and if not, create the attribute by importing it fromPyModTest/Source/WildMod.pyWildFunc(which it does)Some relevant details are discussed in PEP 302 and in the Python language reference.
If you don’t want to have a variable named
Source, that’s easy to fix: justdel Sourceafter you import the function. But bear in mind that it will prevent any code that runs later on from accessingPyModTest.Source.<anything>(except forWildFunc, since you have saved a reference to that). I would definitely suggest just ignoring the reference toSource, not deleting it, since it’s not hurting anything.