Aptus has dependencies on three large packages, wxPython, Numpy, and PIL. The simple thing to do would be to import the modules and use the methods I need. But if the module is missing, an unhelpful ImportError message is all you get. And if the module is present, but isn’t recent enough, then the method call may fail with a missing name.
To solve these problems, I use this helper function instead:
""" Import modules in a helpful way, raising detailed exceptions
if the module can't be found or isn't the proper version.
if name == 'wx':
url = "http://wxpython.org/"
raise Exception("Need wxPython, from " + url)
if not hasattr(wx, 'BitmapFromBuffer'):
raise Exception("Need wxPython 2.8 or greater, from " + url)
elif name == 'numpy':
url = "http://numpy.scipy.org/"
raise Exception("Need numpy, from " + url)
elif name == 'Image':
url = "http://pythonware.com/products/pil/"
raise Exception("Need PIL, from " + url)
if not hasattr(Image, 'fromarray'):
raise Exception("Need PIL 1.1.6 or greater, from " + url)
Then, instead of “import wx”, I use:
wx = importer("wx")
and if anything goes wrong, the exception includes helpful details.
This technique still suffers from the problem of detecting that the module is actually missing. Because of Python’s impoverished exceptions, catching ImportError doesn’t necessarily mean that the module was missing, although that’s the most likely reason.