Better error message on recursive import

T

Thomas Guettler

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
 
M

Marc 'BlackJack' Rintsch

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
 
T

Thomas Guettler

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
 
M

Marc 'BlackJack' Rintsch

===> 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top