Testing the availability of a module

Steven D'Aprano steve at REMOVETHIScyber.com.au
Mon Dec 19 10:42:42 EST 2005


On Mon, 19 Dec 2005 08:33:18 -0600, Bo Peng wrote:

> This is used in a scenario like:
> 
>    if wxPython is available:
>      prepare wx based GUI
>    elif Tkinter is available:
>      prepare tk based GUI
>    else:
>      command line only
> 
> Since a user may prefer command line so wxPython/tkinter should not be 
> actually loaded before the GUI function is called.

Since that is a choice, it shouldn't depend on the availability of a
module:


if user_preferences["commandline"]:
    build CLI
    run CLI
else:
    try:
        import wxPython
        build wx GUI
    except ImportError:
        try:
            import tkinter
            build tk GUI
        except ImportError:
            raise NoAvailableGuiError
    run GUI


-- 
Steven.




More information about the Python-list mailing list