471,336 Members | 976 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,336 software developers and data experts.

Circular import problem


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.

Jul 13 '07 #1
6 4399
En Thu, 12 Jul 2007 23:36:16 -0300, bvdp <bo*@mellowood.caescribió:
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

Jul 13 '07 #2
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.

Jul 13 '07 #3
En Fri, 13 Jul 2007 13:24:57 -0300, bvdp <bo*@mellowood.caescribió:
>
>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)
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

Jul 14 '07 #4
bvdp wrote:
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
Jul 15 '07 #5
En Sat, 14 Jul 2007 14:44:05 -0300, bvdp <bo*@mellowood.caescribió:
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'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
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

Jul 15 '07 #6
On Jul 14, 6:27 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Fri, 13 Jul 2007 13:24:57 -0300, bvdp <b...@mellowood.caescribió:


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.
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.
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)
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

Jul 15 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Frantisek Fuka | last post: by
1 post views Thread by Chris S. | last post: by
4 posts views Thread by Achim Domma (Procoders) | last post: by
9 posts views Thread by ajikoe | last post: by
2 posts views Thread by Vera | last post: by
1 post views Thread by Learning Python | last post: by
1 post views Thread by dotnetnewbie | last post: by
3 posts views Thread by Solution Seeker | last post: by
reply views Thread by guy | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.