Doubley imported module caused devastating bug

Francis Carr coldtortuga at gmail.com
Sun Sep 27 14:42:28 CEST 2009


> I would like to propose that it be made impossible in the Python
> source to import two instances of the same module.

A fully-automatic solution is more difficult than it might seem at
first:
  http://www.python.org/dev/peps/pep-0328/
But there is a simple code-discipline solution: never ever use
relative imports, even between code in the same package.

We got "bit" by double-imports (a mix of relative, absolute, and even
cross-imports A "import B" and B "import A") early on in one of our
projects.  The symptom was that the imported module would be
initialized *twice*, once for a relative import and once for an
absolute.  This is not a happy situation for pseudo-singletons like
the "logging" module --- esp. if one is hacking the internals! :-)  We
no longer use relative imports *EVER*, even within the same package.

Perhaps Perforce is doing something tricky with scoping or importing,
and you've just managed to stumble across this trickiness because of a
double-import.  Good luck, these things are a bugger to debug.



More information about the Python-list mailing list