Connecting Tech Pros Worldwide Help | Site Map

Circular import problem

  #1  
Old July 13th, 2007, 03:45 AM
bvdp
Guest
 
Posts: n/a

I'm going quite nutty here with an import problem. I've got a fairly
complicated program (about 12,000 lines in 34 modules). I just made
some "improvements" and get the following error:

bob$ mma
Traceback (most recent call last):
File "/usr/local/bin/mma", line 55, in <module>
import MMA.main
File "/usr/local/share/mma/MMA/main.py", line 33, in <module>
import MMA.docs
File "/usr/local/share/mma/MMA/docs.py", line 34, in <module>
import MMA.grooves
File "/usr/local/share/mma/MMA/grooves.py", line 41, in <module>
import MMA.auto
File "/usr/local/share/mma/MMA/auto.py", line 35, in <module>
import MMA.parse
File "/usr/local/share/mma/MMA/parse.py", line 2052, in <module>
'AUTHOR': MMA.docs.docAuthor,
AttributeError: 'module' object has no attribute 'docs'

I can fix this by deleting a line in docs.py. Just take out the
"import MMA.grooves" and all works. What I really don't get is that in
the middle of this module which is now NOT loading "grooves.py" I have
a command to access a function in that module and it works.

Yes, grooves.py has an import docs.py. And there are lots of other
such imports in the program.

So, I have to assume that the error is being generated by some other
modules loading in the grooves.py stuff ... but really have no idea.

Is there anything I can do to trace though this and get it working
properly?

Oh, this is python 2.5.1 on Ubuntu Linux.

Thanks.

  #2  
Old July 13th, 2007, 04:15 AM
Gabriel Genellina
Guest
 
Posts: n/a

re: Circular import problem


En Thu, 12 Jul 2007 23:36:16 -0300, bvdp <bob@mellowood.caescribió:
Quote:
I'm going quite nutty here with an import problem. I've got a fairly
complicated program (about 12,000 lines in 34 modules). I just made
some "improvements" and get the following error:
>
bob$ mma
Traceback (most recent call last):
File "/usr/local/bin/mma", line 55, in <module>
import MMA.main
File "/usr/local/share/mma/MMA/main.py", line 33, in <module>
import MMA.docs
File "/usr/local/share/mma/MMA/docs.py", line 34, in <module>
import MMA.grooves
File "/usr/local/share/mma/MMA/grooves.py", line 41, in <module>
import MMA.auto
File "/usr/local/share/mma/MMA/auto.py", line 35, in <module>
import MMA.parse
File "/usr/local/share/mma/MMA/parse.py", line 2052, in <module>
'AUTHOR': MMA.docs.docAuthor,
AttributeError: 'module' object has no attribute 'docs'
>
I can fix this by deleting a line in docs.py. Just take out the
"import MMA.grooves" and all works. What I really don't get is that in
the middle of this module which is now NOT loading "grooves.py" I have
a command to access a function in that module and it works.
>
Yes, grooves.py has an import docs.py. And there are lots of other
such imports in the program.
>
So, I have to assume that the error is being generated by some other
modules loading in the grooves.py stuff ... but really have no idea.
>
Is there anything I can do to trace though this and get it working
properly?
See http://effbot.org/zone/import-confusion.htm
Try to move the circular references later in the code (maybe inside a
function, when it is required), or much better, refactor it so there is no
circularity.

--
Gabriel Genellina

  #3  
Old July 13th, 2007, 05:25 PM
bvdp
Guest
 
Posts: n/a

re: Circular import problem


Quote:
Seehttp://effbot.org/zone/import-confusion.htm
Try to move the circular references later in the code (maybe inside a
function, when it is required), or much better, refactor it so there is no
circularity.
>
--
Gabriel Genellina
Yes, thanks. I'd read that page before posting. Helpful.

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.

I have tried to delay the import, and that does work. But, from a
stylistic view I really to like to have all my imports at the top of
the module. Maybe some old assembler/C habits on my part.



  #4  
Old July 14th, 2007, 04:35 AM
Gabriel Genellina
Guest
 
Posts: n/a

re: Circular import problem


En Fri, 13 Jul 2007 13:24:57 -0300, bvdp <bob@mellowood.caescribió:
Quote:
>
Quote:
>Seehttp://effbot.org/zone/import-confusion.htm
>Try to move the circular references later in the code (maybe inside a
>function, when it is required), or much better, refactor it so there is
>no
>circularity.
>>
>--
>Gabriel Genellina
>
Yes, thanks. I'd read that page before posting. Helpful.
>
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.
But if MMA is a package, and MMA.grooves is a module inside that package,
you need to import it first (unless MMA/__init__.py does that already)
Quote:
I have tried to delay the import, and that does work. But, from a
stylistic view I really to like to have all my imports at the top of
the module. Maybe some old assembler/C habits on my part.
Sure, it's considered good style.

--
Gabriel Genellina

  #5  
Old July 15th, 2007, 02:35 AM
greg
Guest
 
Posts: n/a

re: Circular import problem


bvdp wrote:
Quote:
before I moved other
imports around I was able to do the following:
>
1. NOT include MMA.gooves,
2. call the function MMA.grooves.somefunc()
>
and have it work.
The import doesn't necessarily have to be in the same module
where the attribute is used. The first time *any* module
does 'import MMA.grooves', a grooves attribute gets put
into MMA, which can be used by any module that has a
reference to MMA.

It's probably not a good idea to rely on this, though,
and better to put an explicit 'import MMA.grooves' into
every module that uses it. Importing something more than
once does no harm.

--
Greg
  #6  
Old July 15th, 2007, 05:05 AM
Gabriel Genellina
Guest
 
Posts: n/a

re: Circular import problem


En Sat, 14 Jul 2007 14:44:05 -0300, bvdp <bob@mellowood.caescribió:
Quote:
Quote:
Quote:
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.
Quote:
I've just used an empty __init__.py file. I will have to read up a bit
more on packages and see what advantage there is to import in that.
__init__.py is executed inside the package's namespace - whatever you
import/define there, is available later as packagename.zzz
Quote:
Which is considered good style? Loading at the top or loading when
needed?
I mean, importing at the top of the module is the recommended practice.
See http://www.python.org/dev/peps/pep-0008/
I try only to import locally: a) when it is slow and not always needed,
and b) when the imported module is unrelated to the main purpose (e.g.
import traceback inside some exception handlers).

--
Gabriel Genellina

  #7  
Old July 15th, 2007, 12:55 PM
Alex Popescu
Guest
 
Posts: n/a

re: Circular import problem


On Jul 14, 6:27 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
Quote:
En Fri, 13 Jul 2007 13:24:57 -0300, bvdp <b...@mellowood.caescribió:
>
>
>
>
>
Quote:
Quote:
Seehttp://effbot.org/zone/import-confusion.htm
Try to move the circular references later in the code (maybe inside a
function, when it is required), or much better, refactor it so there is
no
circularity.
>
Quote:
Quote:
--
Gabriel Genellina
>
Quote:
Yes, thanks. I'd read that page before posting. Helpful.
>
Quote:
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?

tia,

../alex
--
..w( the_mindstorm )p.
Quote:
But if MMA is a package, and MMA.grooves is a module inside that package,
you need to import it first (unless MMA/__init__.py does that already)
>
Quote:
I have tried to delay the import, and that does work. But, from a
stylistic view I really to like to have all my imports at the top of
the module. Maybe some old assembler/C habits on my part.
>
Sure, it's considered good style.
>
--
Gabriel Genellina

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
circular import problem Learning Python answers 1 September 9th, 2005 08:05 PM
circular import Module ajikoe@gmail.com answers 9 July 19th, 2005 02:59 AM
Import problem Achim Domma (Procoders) answers 4 July 18th, 2005 06:59 PM
Basic 'import' problem Frantisek Fuka answers 9 July 18th, 2005 09:16 AM