python import package - subpackage should not show up in symbol table

前端 未结 1 902
粉色の甜心
粉色の甜心 2021-02-06 01:50

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:

相关标签:
1条回答
  • 2021-02-06 02:53

    Two things to note here:

    • In Python, modules are actual objects, and the dot that appears between their names represents an actual attribute access
    • You are doing a relative import, meaning that Source is actually PyModTest.Source (thanks to TokenMacGuy for pointing this out)

    So: in order to import PyModTest.Source.WildMod.WildFunc, Python has to

    1. import PyModTest (which was already done by you)
    2. check and see if it has an attribute called Source, and if not, create the attribute by importing it from PyModTest/Source/__init__.py
    3. check and see if that has an attribute called WildMod, and if not, create the attribute by importing it from PyModTest/Source/WildMod.py
    4. check and see if that has an attribute called 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.

    0 讨论(0)
提交回复
热议问题