Stop propagating if import wx fails

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

···

On Monday, July 13, 2015 at 5:58:31 AM UTC-4, 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.

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

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
···

On Tuesday, July 14, 2015 at 11:41:41 AM UTC-4, Boštjan Mejak wrote:

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?

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.

···

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

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.

···

On Thursday, July 16, 2015 at 7:45:06 AM UTC-4, 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?