Stop propagating if import wx fails

65 views
Skip to first unread message

Boštjan Mejak

unread,
Jul 13, 2015, 5:58:31 AM7/13/15
to wxPython Help
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.")

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?

Tim Roberts

unread,
Jul 13, 2015, 1:31:08 PM7/13/15
to wxpytho...@googlegroups.com
Boštjan Mejak wrote:
> 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.")
>
> 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.

You are misdiagnosing the situation. This is not a case of error
propagation. "Name 'wx' is not defined" is a brand-new and entirely
correct error. If you delete the first four lines completely and do
nothing but:

class MyFrame(wx.Frame):
pass

you would get exactly the same error, and for exactly the same reason.


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

If you fail to import wx, how can you possibly hope to drive a class
from wx.Frame?

--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Chris Barker

unread,
Jul 13, 2015, 1:56:52 PM7/13/15
to wxpython-users
On Mon, Jul 13, 2015 at 2:58 AM, Boštjan Mejak <bostjan...@gmail.com> wrote:
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





 
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-user...@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....@noaa.gov

Mike Stover

unread,
Jul 14, 2015, 11:14:48 AM7/14/15
to wxpytho...@googlegroups.com
As Chris stated, your Try/Except block is only issuing a print when the exception occurs. I normally use something along the lines of:

    try:
        import wx
    except ImportError as e:
        print("%s" % e) # print only used for demonstration, I normally log errors like this to a file
        sys.exit(1)

-Mike S

Boštjan Mejak

unread,
Jul 14, 2015, 11:41:41 AM7/14/15
to wxPython Help

I forgot to mention that I create a message box in which I tell the user that he/she doesn't have wxPython installed and inform the user to install it. This message box is in the except block.
ctypes.windll.user32.MessageBoxA is the thing I use as the message box. But that message box is never shown. I immediately get the NameError exception that "name 'wx' is not defined" because Python goes and sees the line class MyFrame(wx.Frame) and throws me that NameError. So how can I make this work? I wanna show that message box. Any ideas?

Mike Stover

unread,
Jul 14, 2015, 2:34:39 PM7/14/15
to wxpytho...@googlegroups.com
See if this helps you out, it works for me with Python 2.7, wxPython 2.9.5 on Win 7 64bit.

import sys

try:
    import wx_
except ImportError as e:
    import ctypes
    messageBox = ctypes.windll.user32.MessageBoxA
    returnValue = messageBox(None, '%s' % e, 'ERROR!', 0x40|0x0)
    sys.exit(0)

class MyFrame(wx.Frame):
    pass


def RunMyProg():
    prog = wx.App()
    frame = MyFrame()
    frame.Show()
    prog.MainLoop()

if __name__ == '__main__':
    RunMyProg()

Essentially what this little code snippet does is attempt to import wx_, which we know will cause an ImportError. Then it goes ahead and import ctypes, creates the message box with the exception error and displays the message. When the user clicks ok it automatically exits the script.

- Mike S

Boštjan Mejak

unread,
Jul 14, 2015, 3:36:29 PM7/14/15
to wxPython Help

Thanks. I'll try the code when I come back from work.

Boštjan Mejak

unread,
Jul 16, 2015, 7:45:06 AM7/16/15
to wxPython Help
Yeah, my code works now. Thanks. I just need one more question answered. What are the keyword arguments for the MessageBoxA() function?

Tim Roberts

unread,
Jul 16, 2015, 2:04:42 PM7/16/15
to wxpytho...@googlegroups.com
Boštjan Mejak wrote:
> Yeah, my code works now. Thanks. I just need one more question
> answered. What are the keyword arguments for the MessageBoxA() function?

That's a raw Windows API. I don't think ctypes supports keyword
arguments. If you want more information on the arguments, you can
Google the MSDN documentation.

Mike Stover

unread,
Jul 20, 2015, 12:28:14 PM7/20/15
to wxpytho...@googlegroups.com
I don't use ctypes enough to know what can or cannot be done with the library, but as Tim suggested you should Google the MSDN documentation.

Boštjan Mejak

unread,
Jul 20, 2015, 3:51:29 PM7/20/15
to wxPython Help

Don't bother, I used constants to make the calling function more descriptive. ;)

Reply all
Reply to author
Forward
0 new messages