473,396 Members | 1,804 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,396 software developers and data experts.

Mutual interdependency problem

I'm getting problems with modules with interdependencies when using the
import form "import foo.bar as bar".

Here are a set of files which replicate the problem

::::::::::::::
importproblem/__init__.py
::::::::::::::
# This is blank

::::::::::::::
importproblem/dir1/__init__.py
::::::::::::::
from bar import *

::::::::::::::
importproblem/dir1/bar.py
::::::::::::::
import importproblem.dir2 as foo
def hello():
print "Hello world"

::::::::::::::
importproblem/dir2/__init__.py
::::::::::::::
from foo import *

::::::::::::::
importproblem/dir2/test.py
::::::::::::::
import importproblem.dir1.bar as bar
def hello():
print "Hello world"

::::::::::::::
importproblem/dir2/foo.py
::::::::::::::
import importproblem.dir1 as dir1
def hello():
print "Hello world"

If you now do
import importproblem.dir1

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "importproblem/dir1/__init__.py", line 1, in ?
from bar import *
File "importproblem/dir1/bar.py", line 1, in ?
import importproblem.dir2 as foo
File "importproblem/dir2/__init__.py", line 1, in ?
from foo import *
File "importproblem/dir2/foo.py", line 1, in ?
import importproblem.dir1 as dir1
AttributeError: 'module' object has no attribute 'dir1'

[GCC 4.1.0 20060210 (Red Hat 4.1.0-0.24)]

If you remove the "as dir1" from the import line in dir2/foo.py then this
works. Can this be explained or fixed?

Maybe the "from foo import *" style in __init__.py is a bad idea, but it
allows you to expose a flat namespace without having to put all the code
into one file. Then naturally you need mutual interdependencies, and then
it breaks! I can work around it by removing the "as XXX" parts on the
import statement, but it gets annoying having to specify the full path.

Jeremy

--
Jeremy Sanders
http://www.jeremysanders.net/
Jun 7 '06 #1
1 1324
Jeremy Sanders wrote:
I'm getting problems with modules with interdependencies when using the
import form "import foo.bar as bar".

[snip]

Maybe the "from foo import *" style in __init__.py is a bad idea, but it
allows you to expose a flat namespace without having to put all the code
into one file. Then naturally you need mutual interdependencies, and then
it breaks! I can work around it by removing the "as XXX" parts on the
import statement, but it gets annoying having to specify the full path.


What worries me is that you have two modules that import each other.
Are you sure you can't do it any other way?

AdSR

Jun 7 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Morten Gulbrandsen | last post by:
Hello Programmers, Thank you for the kind advice, I have another small problem with MySQL I'd like to investigate, All I get is this:=20 ERROR 1005 at line 47:=20 Can't create table...
0
by: Morten Gulbrandsen | last post by:
Hello Programmers, Thank you for the kind advice, I have another small problem with MySQL I'd like to investigate, All I get is this:=20 ERROR 1005 at line 47:=20 Can't create table...
5
by: clusardi2k | last post by:
Hello, I have a assignment just thrown onto my desk. What is the easiest way to solve it? Below is a brief description of the task. There are multible programs which use the same library...
0
by: Support | last post by:
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40"> <head>...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.