howto check does module 'asdf' exist? (is available for import)

Asun Friere afriere at yahoo.co.uk
Tue May 22 03:27:04 EDT 2007


On May 21, 11:17 pm, dmitrey <open... at ukr.net> wrote:
> howto check does module 'asdf' exist (is available for import) or no?
try :
  import asdf
  del asdf
except ImportError :
  print "module asdf not available"
else :
  print "module asdf available for loading"

You can generalise this, but at the expense of a couple of exec
statements:
def is_module_available (module) :
    try :
        exec('import %s' % module)
        exec('del %s' % module)
    except ImportError :
        return False
    else :
        return True

> (without try/cache of course)
Oops sorry, you wanted it done in some non-obvious way!  Why?!




More information about the Python-list mailing list