Testing the availability of a module

Erik Max Francis max at alcyone.com
Mon Dec 19 20:26:22 CET 2005


Bo Peng wrote:

> Is there a better way than doing
> 
> try:
>    import aModule
> except:
>    has_aModule = False
> else:
>    has_aModule = True
> 
> The main concern here is that loading aModule is unnecessary (and may 
> take time).

Yes.  Specifically catch ImportError in your except clause.

-- 
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
   Substance is one of the greatest of our illusions.
   -- Sir Arthur Eddington



More information about the Python-list mailing list