ImportError: cannot import name dns

Prasad, Ramit ramit.prasad at jpmorgan.com
Fri Sep 16 13:52:33 EDT 2011


NOTE: This is all speculation on my part. I am hoping someone on the list will give us more concrete details.

1. check for "dns" in sys.modules (initially not found)
2. create new empty module, add it to sys.modules as "dns"
3. execute dns.py in new module namespace (executes "from foo import udp") 
It has not yet added anything defined in dns because it is still on import statement

4. check for "udp" in sys.modules (not found)
5. create new empty module, add it to sys.modules as "udp"
6. execute udp.py in new module namespace (executes "from foo import dns") 
Cannot do this because dns is still not defined even though it the name is in sys.modules and since we are still in the middle of the first statement defining dns, it raises an error.


7. check for "dns" in sys.modules (found!)
8. done executing udp.py
9. done executing dns.py

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423



-----Original Message-----
From: Jack Bates [mailto:jack.bates at gmail.com] 
Sent: Wednesday, September 14, 2011 11:22 AM
To: Prasad, Ramit
Cc: python-list at python.org
Subject: Re: ImportError: cannot import name dns

> It is a circular dependency. Dns will try to import udp which will in turn import dns (again) in an endless cycle; instead an ImportError is raised.
>
> Circular dependency is a Bad Thing.

According to this documentation:

http://docs.python.org/reference/simple_stmts.html#grammar-token-import_stmt

http://effbot.org/zone/import-confusion.htm

 - I thought Python would do something like:

So I'd expect attempting to access symbols from "dns" while executing
udp.py to fail, because dns.py isn't done executing at this point.
However I don't attempt to access any symbols from "dns" - so I don't
expect this ImportError

What is my mistake?
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  



More information about the Python-list mailing list