The difference between an imported and a loadable module is what fits into your current module namespace. The module is loaded only once (in normal situations), but can be imported many times from different places. A loaded module may not be available in this namespace if it was not imported there. For example, you can load a module without importing it under your name using the syntax from module import name (you can access the specified name, but not the module itself).
You see the os module in the sys.modules dictionary because it is used internally by the python interpreter, and therefore it always loads at startup. You cannot access it using the name "os", though, since it is not automatically imported into your namespace.
However, you can get around the usual import mechanisms in several ways. For example, try the following:
import sys os = sys.modules["os"]
Now you can access the os module in the same way as if you had done import os .
This is because this code is exactly what the import statement does when you request a module that has already been loaded. However, if you try the code above with a module that has not yet been loaded, it will not work (you will get a key error from the sys.modules dictionary). The import statement loads new modules in addition to adding them to the current namespace. Although you can manually load modules and work with a regular import system, there are rarely good reasons for this.
Blckknght
source share