Circular import problem

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sun Jul 15 15:08:49 EDT 2007


En Sun, 15 Jul 2007 08:49:54 -0300, Alex Popescu  
<the.mindstorm.mailinglist at gmail.com> escribió:

>> > But, I still don't understand how python can access a function in a
>> > file I have NOT included. In this case, to get things to work, I DO
>> > NOT "import MMA.grooves" but later in the module I access a function
>> > with "xx=MMA.grooves.somefunc()" and it finds the function, and works
>> > just fine. It shouldn't work.
>>
>> That depends a bit on what is "MMA" and what is "grooves".
>> MMA.grooves means "look for an attribute named grooves inside MMA". If  
>> MMA
>> is a module, and MMA.grooves is a class/function defined inside the
>> module, you don't need to import it before using it.
>
> I am a bit confused: I think the above should be:
>
> if MMA.grooves in a class/function defined inside the module MMA, you
> don't need to import the
> class/function before using it, but only import the module MMA.
>
> Am I wrong?

You are right, but we were talking about a slightly different scenario. To  
make things clear:

1)
import MMA
xx=MMA.grooves.somefunc()

2)
import MMA.grooves
xx=MMA.grooves.somefunc()

If MMA is a module, containing a class named gooves, those two ways are  
valid.
If MMA is a package, and grooves is a module contained in the package,  
only the second one is valid (assuming the package has an empty  
__init__.py, or does not import grooves itself)

The OP was asking why the second form worked fine for him for a while, but  
stopped working after reordering some imports. And the answer (according  
to "Greg") is: some other module imported MMA.grooves, and that put  
grooves into MMA namespace, available for everyone else.

-- 
Gabriel Genellina




More information about the Python-list mailing list