Best practices with import?

Tavis Rudd tavis at calrudd.com
Thu Aug 2 16:15:21 EDT 2001


Peter Hansen wrote: 
> 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
(www.cheetahtemplate.org) 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.  We fixed it by moving the import
statement inside the method where the module was needed.

Cheers,
Tavis



More information about the Python-list mailing list