When I start the interactive django shell through manage.py, by executing
python -v manage.py shell
from the project directory, I see a lot of modules of format django.package.module getting imported in the verbose output but still I have to import them to use it in the shell.
The same happens when I just run the Python shell (with the -v argument). For example I see this in the verbose output,
import os # precompiled from /usr/local/gdp/lib/python2.4/os.pyc
but still i have to do import os to import and use the os module. What is being imported that am seeing in the verbose output and why I have to import them explicitly again to use them in the shell? Does Python load some essential modules while starting the shell or is it some kind of behind-the-scene magic?
-vtraces the first import of a module — the one that actually loads the module (executes its code, and so may take a bit of time) and sticks it intosys.modules.That has nothing to do whether your interactive session (module
__main__) gets the module injected into its namespace, of course. To ensure module'goo'does get into the namespace of module'X'(for anyX, so of course including__main__… among many, many others), module'X'just needs toimport gooitself (a very fast operation indeed, ifsys.modules['goo']is already defined!-).