code in a module is executed twice (cyclic import problems) ?

Dave Angel davea at
Mon Oct 12 14:03:25 CEST 2009

greg wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">Dave 
> Angel wrote:
>> The point you should get from that link is
>> "Don't do circular imports.  Ever."
> No, I would say the lesson to be learned from this is
> "don't use the same file as both a main script and an
> imported module".
> I would create another file called e.g. '' that
> simply contains
>   import B
> and use that as the main script.
> Another benefit is that the compiled bytecode of B will
> be saved, so it will start up faster the next time you
> run it. That won't happen if you use B directly as a
> main script.
> (Circular imports are a different issue. There are perfectly
> legitimate uses for them, and they're fine as long as you
> understand the potential pitfalls and take steps to avoid
> them. Although it's probably best to avoid them until you
> do understand all the issues.)
You prove my point by trying to contradict it.   Importing a script from 
the same or another module is a circular import, and therefore should be 
avoided.  If you understand all the issues, you can get away with it, 
but few programmers understand them all like they think they do.

The *only* difference between this circular import and any other is that 
the problems it can trigger are a little more obvious, and a lot more 


More information about the Python-list mailing list