Testing the availability of a module

Bo Peng bpeng at rice.edu
Mon Dec 19 12:02:23 EST 2005


> if the user happens to prefer the command line interface, why bother looking
> for a GUI ?  sounds like you haven't really thought this through...
> 
> 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'):
       wxGUI()
     elif imp.find_module('Tkinter'):
       tkGUI()
     else:
       cml()

After a user loads this module (without wx loaded automatically as 
before), he can call cml() or GUI().

Bo



More information about the Python-list mailing list