I'm wondering about the contents of the sys.modules
dictionary. For example, when I import the compiler
package:
>>> import compiler
>>> for k in sys.modules:
... if k == 'compiler' or k.startswith('compiler.'):
... print k
...
compiler.sys
compiler.ast
compiler.token
compiler
compiler.consts
compiler.warnings
compiler.transformer
compiler.struct
compiler.parser
compiler.symbol
compiler.imp
compiler.visitor
compiler.cStringIO
compiler.os
compiler.compiler
compiler.syntax
compiler.future
compiler.dis
compiler.pycodegen
compiler.misc
compiler.pyassem
compiler.types
compiler.symbols
compiler.marshal
Why are there entries like compiler.warnings
and compiler.marshal
? They are not part of the compiler
package nor actually attributes of the module:
>>> for k in sys.modules:
... if k.startswith('compiler.') and hasattr(compiler, k.split('.')[1]):
... print k
...
compiler.ast
compiler.consts
compiler.warnings
compiler.transformer
compiler.visitor
compiler.syntax
compiler.future
compiler.pycodegen
compiler.misc
compiler.pyassem
compiler.symbols
And the package contents are:
Why are the "extra items" in the sys.modules
dictionary and what are they for? Modules like marshal
are not part of the compiler
package but are prefixed with 'compiler.'
and I don't see why. Additionally, there's an entry for the marshal
module in sys.modules
after importing the compiler package.
sys.modules['marshal']
<module 'marshal' (built-in)>
Note: I picked the marshal randomly, it serves as a general replacement for any module.