472,328 Members | 1,156 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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
4 1535
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Stian Søiland | last post by:
all examples performed with: Python 2.3+ (#2, Aug 10 2003, 11:09:33) on linux2 (2, 3, 0, 'final', 1) This is a recursive import:
6
by: Maurice LING | last post by:
Hi, I have the following codes: from __future__ import nested_scopes import re from UserDict import UserDict class Replacer(UserDict):
7
by: aurora | last post by:
I love generator and I use it a lot. Lately I've been writing some recursive generator to traverse tree structures. After taking closer look I have...
0
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones...
2
by: sebastien.abeille | last post by:
Hello, I would like to create a minimalist file browser using pyGTK. Having read lot of tutorials, it seems to me that that in my case, the...
2
by: Martin Marcher | last post by:
Hello, I'm playing around with os.walk and I made up del_tree(path) which I think is correct (in terms of the algorithm, but not as python wants...
18
by: Just Another Victim of the Ambient Morality | last post by:
Is pyparsing really a recursive descent parser? I ask this because there are grammars it can't parse that my recursive descent parser would parse,...
10
by: pereges | last post by:
How to to go about this ? Suppose a malloc inside a recursive function has failed and you want to set the error flag and return it to the calling...
3
by: from.future.import | last post by:
Hi, I encountered garbage collection behaviour that I didn't expect when using a recursive function inside another function: the definition of...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.