views:

95

answers:

2
        except ImportError as xcpt:
            print "Import Error: " + xcpt.message

Gets you a deprecation warning in 2.6 because message is going away. Stackoverflow

How should you be dealing with ImportError? (Note, this is a built-in exception, not one of my making....)

+4  A: 

The correct approach is

xcpt.args

Only the message attribute is going away. The exception will continue to exist and it will continue to have arguments.

Read this: http://www.python.org/dev/peps/pep-0352/ which has some rational for removing the messages attribute.

S.Lott
+2  A: 

If you want to print the exception:

print "Couldn't import foo.bar.baz: %s" % xcpt

Exceptions have a __str__ method defined to create a readable version of themselves. I wouldn't bother with "Import Error:" since the exception will provide that itself. If you add text to the exception, make it be something you know based on the code you were trying to execute.

Ned Batchelder
When I doprint xcpt, I get "No module named tribes.models" which does not give me the exception title, even though it is implicit in the message.
Mark0978