Best practices with import?

Peter Hansen peter at
Mon Aug 6 06:01:59 CEST 2001

Tavis Rudd wrote:
> Peter Hansen wrote:

Umm.. no I didn't.  Gordon wrote this...

> > It's also very useful in some circular import situations.
> >
> > Circular imports are fine where both modules use the "import <module>"
> > form of import. They fail when the 2nd module wants to grab a name
> > out of the first ("from module import name") and the import is at
> > the top level. That's because names in the 1st are not yet available,
> > (the first module is busy importing the 2nd).
> >
> > As above, this is a sensible solution only if the use of the imported
> > module is confined to the one function.
> Actually, circular imports can still cause problems using the "import
> <module>" form. Some of the "import  <module>" statements in Cheetah
> ( were causing circular import problems in
> Jython.  cPython was fine, unless the module was being imported using
> imp.load_source() or one of its kin.  

This sounds like Jython is broken.  Shouldn't the import statement 
in the second module, which imports the first module, just bring
a reference into the second module's namespace?  It's probably not
supposed to cause any trouble.

imp.load_source(), I note, is documented as "obsolete".  It also 
points out, however, that it will re-initialize the module if it
is already loaded, which implies it won't work with circular 
references, as you discovered.  Maybe Jython is using load_source() 
or an equivalent, without looking in sys.modules first?

Peter Hansen, P.Eng.
peter at

More information about the Python-list mailing list