Stop propagating if import wx fails

The code is worth a thousand words, so here are my thousand words:

try:
    import wx_ # Intentionally added the trailing underscore to invoke
ImportError
except ImportError:
    print("You don't seem to have wxPython installed.")

you have just handled the Exception, so you are telling python to keep gng
and try to run the rest of the code. Which, of course, it can't do, bcause
wx was not imported.

try:
    import wx_ # Intentionally added the trailing underscore to invoke
ImportError
except ImportError:

    print("You don't seem to have wxPython installed.")
    raise

Should do it -- but the IMPort Error will already give a message that the
wx module can't be found, so this doesn't buy you much.

Unless you provide a longer an more interesting message, like pointers to
how to install wx.

-Chris

···

On Mon, Jul 13, 2015 at 2:58 AM, Boštjan Mejak <bostjan.xperia@gmail.com> wrote:

class MyFrame(wx.Frame):
    pass

I have a problem that even though the ImportError exception is properly
handled the propagation does not stop there but propagates to class
MyFrame(wx.Frame) and then throws the NameError exception at me, saying
"Name 'wx' is not defined.". It doesn't print the "You don't seem to have
wxPython installed." message at all.

How can I fix my code so that if an ImportError is caught the propagation
to class MyFrame(wx.Frame) does not happen?

--
You received this message because you are subscribed to the Google Groups
"wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to wxpython-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov