2

I'm wondering about the contents of the sys.modules dictionary. For example, when I import the compilerpackage:

>>> 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:

Windows explorer showing the contents of the compiler package

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.

Niklas R
  • 16,299
  • 28
  • 108
  • 203
  • Interesting indeed; the extra modules are created as you import modules in the `compiler` package, and always have `None` as their value. Each of the 'extra' modules is something that is used by the `compiler` package. – Martijn Pieters Mar 14 '13 at 15:02
  • 1
    If you read `.../Python/Lib/compiler/__init__.py` you'll see that it `imports` several other modules in the `compiler` package which in turn import other modules -- for example `pycodegen` `imports` `marshal`. – martineau Mar 14 '13 at 15:08
  • @martineau: Sure, but that does (at least for me) not clarify why the imported module is additionally to its usual entry stored as `importing_module.imported_module` in `sys.modules`. – Niklas R Mar 14 '13 at 15:11
  • Which version of Python? Could it have something to do with the `__future__.absolute_import` feature? – Dirk Mar 14 '13 at 17:17

0 Answers0