How to test if a module exists?

J

Jon Dufresne

Hi,

My python program has an extension system where the extension can have
a optional magic python modules. Meaning if the extension module
exists, the program will use it and if not, it will continue without
the module. So my program tests if a module exists, if so use it,
otherwise continue. This is how I originally did this (pseudo code):


try:
import extension_magic_module
except ImportError:
pass
else:
handle_extension_magic_module()


However, if the the extension module exists but throws an ImportError,
due to a bug in the extension this idiom will mask the error and I
will never see it. Later on in the program I will get unexpected
behavior because the module never successfully imported. I want the
program to fail if the extension module fails to import, but continue
if the module doesn't exist. Is there a correct way to handle this?


Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top