sys.modules

Chris Angelico rosuav at gmail.com
Thu Feb 21 14:10:43 EST 2019


On Fri, Feb 22, 2019 at 6:03 AM Chris Warrick <kwpolska at gmail.com> wrote:
>
> On Thu, 21 Feb 2019 at 18:57, ast <none at gmail.com> wrote:
> >
> > Hello
> >
> > Is it normal to have 151 entries in dictionary sys.modules
> > just after starting IDLE or something goes wrong ?
> >
> >  >>> import sys
> >  >>> len(sys.modules)
> > 151
> >
> > Most of common modules seems to be already there,
> > os, itertools, random ....
> >
> > I thought that sys.modules was containing loaded modules
> > with import command only.
>
> sys.modules contains all modules that have been imported in the
> current session. Some of those imports happen in the background,
> without your knowledge — for example, because these modules are
> required by the interpreter itself, or are part of IDLE. The number
> you see depends on the environment (I got 530 in ipython3, 34 in
> python3, 45 in python2) and is not in any way important.
>

The OP is technically correct in that they have been loaded with the
"import" statement (or equivalent). What may not be obvious is that
many modules will import other modules, which also get cached.
Generally this is pretty insignificant, unless you're trying to
benchmark startup performance or something; the module cache just
magically speeds everything up for you.

ChrisA


More information about the Python-list mailing list