[Python-Dev] Odd errors when catching ImportError

Tim Peters tim.one@home.com
Wed, 30 Jan 2002 19:33:48 -0500


[Paul Dubois]
> ...
> I have been running into a number of odd errors caused by code like the
> following. The behavior seems to be machine dependent.

Which version(s) of Python?  (Released, current CVS, all, ...?)

> fooflag = 0
> try:
>     import foo
> except ImportError:
>     fooflag = 1
>
> I have had this result in a seg fault upon exit,

Does or does not "foo" exist?  Or does it segfault both ways?  Either way,
run Python -vv to get a trace of what it's trying during the import attempt.
The last line displayed before the segfault may be a useful clue.  You may
even discover you're really importing a compiled foo extension module with a
hardcoded segfault in module init <wink>.

> and also when something like this was in file xxx.py inside a package,
> and the __init__.py did
>
> from xxx import fooflag
>
> I've had it tell me xxx had no attribute fooflag. I added "print
> fooflag" at the bottom of the file and it fixed it. That was on a DEC.
> On Linux it worked.
>
> I suppose I should be testing for the ability to import foo some other
> way but I don't know what it is.

That's "the usual" way to check imports; if it were a widespread problem
under any version of Python, I expect we would have heard about it before.

If you have useful followups, you should record them in a bug report on
SourceForge (Python-Dev is a black hole for bug reports).