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:
Two things to note here:
Source
is actually PyModTest.Source
(thanks to TokenMacGuy for pointing this out)So: in order to import PyModTest.Source.WildMod.WildFunc
, Python has to
PyModTest
(which was already done by you)Source
, and if not, create the attribute by importing it from PyModTest/Source/__init__.py
WildMod
, and if not, create the attribute by importing it from PyModTest/Source/WildMod.py
WildFunc
(which it does)Some relevant details are discussed in PEP 302 and in the Python language reference.
Deeper down in the mechanism, a dotted name import is split up by its components. For "
import spam.ham
", first an "import spam
" is done, and only when that succeeds is "ham
" imported as a submodule of "spam
".
If you don't want to have a variable named Source
, that's easy to fix: just del Source
after you import the function. But bear in mind that it will prevent any code that runs later on from accessing PyModTest.Source.<anything>
(except for WildFunc
, since you have saved a reference to that). I would definitely suggest just ignoring the reference to Source
, not deleting it, since it's not hurting anything.