Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > Python

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-06-2007, 06:38 AM   PM User | #1
xconspirisist
Regular Coder

 
xconspirisist's Avatar
 
Join Date: Jun 2006
Location: Great Britain.
Posts: 137
Thanks: 1
Thanked 6 Times in 6 Posts
xconspirisist has a little shameless behaviour in the past
Post Can you preventing import dependency loops?

I'm having a problem where two modules depend on each other. I am using simple import statements, but it would create an endless loop as the two files try to import each other. Obviously python prevents this, but it only gets to a certain point in each file. This is rather hard to explain by text, so I'll try an explain by example.

You start off by parsing a.py

a.py
Code:
print "a"

import b

def d():
	print "d"
b.py
Code:
print "b"

import a

def c():
	print "c"
	d();
	
c()
This will fail, because d has not yet been defined. The problem is, that code in b.py depends on code in a.py, and vice versa. Here is the output and eventual stacktrace.

Quote:
Originally Posted by stacktrace
a
b
a
c
Traceback (most recent call last):
File "C:\Documents and Settings\xconspirisist\Desktop\a.py", line 3, in <module>
import b
File "C:\Documents and Settings\xconspirisist\Desktop\b.py", line 9, in <module>
c()
File "C:\Documents and Settings\xconspirisist\Desktop\b.py", line 7, in c
d();
NameError: global name 'd' is not defined
Any help would be greatly appreciated
__________________
If I have been helpful, use the "thank" button - It makes me happy!

xconspirisist.co.uk - homepage of my online alias
technowax.net - a community for people interested in all forms of modern technology.

Last edited by xconspirisist; 04-06-2007 at 06:41 AM..
xconspirisist is offline   Reply With Quote
Old 06-14-2007, 04:54 AM   PM User | #2
nytrokiss
New Coder

 
Join Date: Jun 2006
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
nytrokiss is an unknown quantity at this point
Try the from statment
__________________
Watches
nytrokiss is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:23 PM.


Advertisement
Log in to turn off these ads.