Python 2.7 does something very peculiar when you look at it under the hood for importing within a package. Basically, it is storing relative imports of built-ins (among other things) for reasons that I truly cannot understand. Minimal use case is below.
Assume a directory structure in the form:
\BaseFolder
* __init__.py
* MainFile.py
\TestFolder
* __init__.py
* TestModule.py
Both __init__.py are empty. The MainFile.py says only:
import TestFolder.TestModule
import sys
for x in sorted(sys.modules.keys()):
print x
The TestModule.py says:
import os
Running the MainFile.py with Python gives you the list of imported modules. When you look through the keys for the modules, there's a bunch of junk, but you can find the following keys:
TestFolder
TestFolder.TestModule
TestFolder.os
...
os
If you look at the values for those modules, TestFolder.os is None. But why does it exist in the first place? Why would the modules list register a module that has been demonstrated to not exist when it looked for it? I assume that this occurs because the system checks for an "os" library in TestFolder first (hence TestFolder.os), then looks in the built-ins. By why add an entry just because you checked? Does anyone have insight into why Python would do this? Maybe just so it never checks for libraries in those locations again?