Testing the availability of a module
Fredrik Lundh
fredrik at pythonware.com
Mon Dec 19 12:24:27 EST 2005
Bo Peng wrote:
> > here's an outline that matches your use case:
> >
> > if user wants command line:
> > command line only
> > else:
> > if wxPython is available:
> > prepare wx based GUI
> > elif Tkinter is available:
> > prepare tk based GUI
> > else:
> > command line only
>
> This is exactly the logic I use,... Eliminating all unrelated stuff, I
> have something like (thanks for the imp hint):
>
> def wxGUI():
> import wx
> wx...
>
> def tkGUI():
> import Tkinter as tk
> tk....
>
> def cml():
> command line
>
> def GUI():
> if options['cml']:
> cml()
> else:
> if imp.find_module('wx'):
that imp call is completely unnecessary, and means that you'll scan
the path *twice* for each module, instead of once.
this is more efficient:
try:
wxGUI()
except ImportError:
try:
tkGUI()
except ImportError:
cml()
</F>
More information about the Python-list
mailing list