Imports again...

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Apr 9 14:27:43 EDT 2010


En Fri, 09 Apr 2010 13:10:44 -0300, Alex Hall <mehgcap at gmail.com> escribió:

> c:\Python26>python.exe i:\arm\main.pyw
> Traceback (most recent call last):
>   File "i:\arm\main.pyw", line 3, in <module>
>     import arm, network, weather, dict
>   File "i:\arm\arm.py", line 4, in <module>
>     import config
>   File "i:\arm\config.py", line 4, in <module>
>     from main import exitProgram
>   File "i:\arm\main.pyw", line 3, in <module>
>     import arm, network, weather, dict
>   File "i:\arm\network.py", line 4, in <module>
>     arm.ready()
> AttributeError: 'module' object has no attribute 'ready'
>
>
> I realize it may be odd to import from main.pyw, but I do not think
> that could be causing the problem... could it?

Yes, it *is* a problem. Note the traceback sequence: main imports arm, arm  
imports config, config imports arm *again* (which is only partially  
initialized), arm imports network, and network tries to use arm.ready and  
fails.

Try to organize your modules hierarchically, so modules higher in the  
hierarchy may import (and use) other modules lower in the hierarchy, but  
not the other way around.
Doing it that way helps also to make clear the intent of each module (and  
class).
The 'main' script should be at the top of the hierarchy: 'main' may import  
anything, but no one may import 'main'. Put your high-level modules below  
it; they may use other low-level ones.

-- 
Gabriel Genellina




More information about the Python-list mailing list