How to test if a module exists?

Jon Dufresne jon.dufresne at gmail.com
Sat Nov 6 14:50:35 EDT 2010


On Sat, Nov 6, 2010 at 11:35 AM, Chris Rebert <clp2 at rebertia.com> wrote:
> Here's what I came up with:
>
> try:
>     import extension_magic_module
> except ImportError as err:
>    if err.message != "No module named extension_magic_module":
>        raise err
> else:
>    handle_extension_magic_module()
>

It seems less than ideal to tie my program's behavior to what
essentially boils down to a documentation string. Is this the
preferred way to handle this?

Jon



More information about the Python-list mailing list