Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder xconspirisist's Avatar
    Join Date
    Jun 2006
    Location
    Great Britain.
    Posts
    138
    Thanks
    1
    Thanked 6 Times in 6 Posts

    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
    Last edited by xconspirisist; 04-06-2007 at 07:41 AM.
    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.

  • #2
    New Coder
    Join Date
    Jun 2006
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try the from statment


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •