I thought I understood how import worked...

Ben Finney ben+python at benfinney.id.au
Tue Aug 7 15:55:16 CEST 2012

Roy Smith <roy at panix.com> writes:

> So, it appears that you *can* import a module twice, if you refer to
> it by different names! This is surprising.

The tutorial is misleading on this. It it says plainly:

    A module can contain executable statements as well as function
    definitions. […] They are executed only the *first* time the module
    is imported somewhere.


but it doesn't make clear that a module can exist in the ‘sys.modules’
list multiple times under different names.

Care to file a documentation bug <URL:http://bugs.python.org/>
describing this?

> It means that having non-idempotent code which is executed at import
> time is a Bad Thing.

This is true whether or not the above about module imports is true. A
well-designed module should have top level code that performs idempotent

Be thankful that you've discovered this, and apply it well :-)

 \       “See, in my line of work you gotta keep repeating things over |
  `\       and over and over again, for the truth to sink in; to kinda |
_o__)               catapult the propaganda.” —George W. Bush, 2005-05 |
Ben Finney

More information about the Python-list mailing list