467,135 Members | 1,183 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,135 developers. It's quick & easy.

Better error message on recursive import

Hi,

why does Python only raise ImportError if it fails caused by a recursive import?

I know what's wrong. But I guess many beginner don't know what's wrong. I don't
want much, just "RecursiveImportError" instead of "ImportError". Is this possible?

Thomas
--
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de
Sep 11 '08 #1
  • viewed: 1393
Share:
4 Replies
On Thu, 11 Sep 2008 12:43:34 +0200, Thomas Guettler wrote:
why does Python only raise ImportError if it fails caused by a recursive
import?

I know what's wrong. But I guess many beginner don't know what's wrong.
I don't want much, just "RecursiveImportError" instead of "ImportError".
Is this possible?
Can you give an example of such a recursive import you want the special
exception be raised?

Ciao,
Marc 'BlackJack' Rintsch
Sep 11 '08 #2
Hi,
Can you give an example of such a recursive import you want the special
exception be raised?
===cat one.py
from two import testtwo
def testone():
print "one"

===cat two.py
import one
def testtwo():
print "two"

===python one.py
Traceback (most recent call last):
File "one.py", line 1, in <module>
from two import testtwo
File "/mnt/home/tguettler/tmp/rec/two.py", line 1, in <module>
import one
File "/mnt/home/tguettler/tmp/rec/one.py", line 1, in <module>
from two import testtwo
ImportError: cannot import name testtwo
--
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de
Sep 12 '08 #3
On Fri, 12 Sep 2008 09:47:42 +0200, Thomas Guettler wrote:
>Can you give an example of such a recursive import you want the special
exception be raised?

===cat one.py
from two import testtwo
def testone():
print "one"

===cat two.py
import one
def testtwo():
print "two"

===python one.py
Traceback (most recent call last):
File "one.py", line 1, in <module>
from two import testtwo
File "/mnt/home/tguettler/tmp/rec/two.py", line 1, in <module>
import one
File "/mnt/home/tguettler/tmp/rec/one.py", line 1, in <module>
from two import testtwo
ImportError: cannot import name testtwo
This is an awkward situation anyway because here are *three* modules
involved. You start `one.py` which will be imported as `__main__`.
`__main__` imports `two` and `two` imports a *new* module `one`! Which
tries to import `testtwo` from `two` which doesn't exist at that time.
Even if you rearrange the code to load properly `one.py` is loaded and
executed *twice* and you end up with two distinct modules generated from
that file.

Ciao,
Marc 'BlackJack' Rintsch
Sep 12 '08 #4
On Thu, 11 Sep 2008 12:43:34 +0200, Thomas Guettler wrote:

Hello,
why does Python only raise ImportError if it fails caused by a recursive import?

I know what's wrong. But I guess many beginner don't know what's wrong.
I don't think that you're right here. I can't remember any beginner
asking such a question on p.c.py. I think they would come and ask
if it really was problematic.

And, anyway, I don't know how to answer your question :-)

--
Regards,
Wojtek Walczak,
http://tosh.pl/gminick/
Sep 12 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Stian Søiland | last post: by
6 posts views Thread by Maurice LING | last post: by
7 posts views Thread by aurora | last post: by
reply views Thread by erik.erikson@gmail.com | last post: by
2 posts views Thread by sebastien.abeille@gmail.com | last post: by
2 posts views Thread by Martin Marcher | last post: by
18 posts views Thread by Just Another Victim of the Ambient Morality | last post: by
10 posts views Thread by pereges | last post: by
3 posts views Thread by from.future.import@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.