473,395 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 4559
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Frantisek Fuka | last post by:
This thing keeps bugging me. It's probably some basic misunderstanding on my part but I am stumped. Let's say I have two Python files: file.py and file2.py. Their contents is as follows: ...
1
by: Chris S. | last post by:
Consider the sample case: ## a.py import d import b b.App() ## b.py from c import C B = 'B'
4
by: Achim Domma (Procoders) | last post by:
Hi, in my __init__.py of module A.B if have something like: from win32com.client import Dispatch obj=Dispatch('...') VERSION=tuple() del obj Now I should be able to write:
9
by: ajikoe | last post by:
Hello, I have two modules (file1.py and file2.py) Is that ok in python (without any weird implication) if my module import each other. I mean in module file1.py there exist command import file2...
2
by: Vera | last post by:
I have two assemblies that each consist of several classes. Each object instantiated from those classes can have one or more child- and/or parentobjects that are also instantiated from those...
1
by: Learning Python | last post by:
An example in the book I didn't understood well two modules files recursively import/from each other in recur1.py,we have: x=1 import recur2 y=1
1
by: dotnetnewbie | last post by:
Hi all I am new to .NET and webservice so I wish someone can shed some light on me. I have a Project class and a Product class, the Project can contain multiple Products (as an ArrayList). In...
3
by: Solution Seeker | last post by:
Hi All, I am here with a Query and need a Solution for it. The Query is as Follows, We have 3 Projects in a Solution - Say UI, CMN and PRD First One Deals with UI Forms Second One Deals...
0
by: guy | last post by:
I have a function below that works fine, however if i try and move it to a different solution and refernce it I have to change it to pass in the assembly , as otherwise it would look in the wrong...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.