How to test if a module exists?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: How to test if a module exists?
On Sat, Nov 6, 2010 at 11:35 AM, Chris Rebert 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: How to test if a module exists?
On Sat, Nov 6, 2010 at 1:52 PM, Roy Smith wrote: > > import sys > try: > import xx > except ImportError: > tb = sys.exc_traceback > while tb: > print tb > tb = tb.tb_next > I went ahead and implemented this and it now works. I even uncovered a bug I wasn't previously seeing because now the program was failing early! :) I hope there isn't a hidden naivete in using this pattern. Thanks, Jon -- http://mail.python.org/mailman/listinfo/python-list
Re: How to test if a module exists?
On Tue, Nov 9, 2010 at 4:30 AM, Roy Smith wrote: > In article , > Lawrence D'Oliveiro wrote: > >> In message , Roy Smith wrote: >> >> > On the other hand, if your module's bug is that it in turn imports some >> > other module, which doesn't exist, you'll also get an ImportError. >> >> Does it really matter? Either way, the module is unusable. > > If I understand correctly, he's trying to differentiate between: > > 1) The module is unusable because it's not installed > > 2) The module is unusable because it has a bug in it. > Yes. Case 1 is an acceptable case. In case 1, if the extension module (extension as in extension to my core program) does not have magic.py then the extension adds no behavior in the magic1 portion of the program. However, if the extension does have magic.py but it is buggy (case 2), I want my program to fail. Currently I am doing this using the traceback code suggested in an earlier post. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to test if a module exists?
On Mon, Nov 8, 2010 at 11:35 PM, Lawrence D'Oliveiro wrote: > In message , Roy Smith wrote: > >> On the other hand, if your module's bug is that it in turn imports some >> other module, which doesn't exist, you'll also get an ImportError. > > Does it really matter? Either way, the module is unusable. In my program it matters. If the module exists but is buggy, then I want to be notified immediately to fix it. However if the module does not exist in an extension module (keyword being extension) then the extensions doesn't add behavior to that part of the program. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to test if a module exists?
On Wed, Nov 10, 2010 at 1:50 AM, Lawrence D'Oliveiro wrote: > In message , Jon > Dufresne wrote: > >> On Mon, Nov 8, 2010 at 11:35 PM, Lawrence D'Oliveiro ... > > I see that you published my unobfuscated e-mail address on USENET for all to > see. I obfuscated it for a reason, to keep the spammers away. I'm assuming > this was a momentary lapse of judgement, for which I expect an apology. > Otherwise, it becomes grounds for an abuse complaint to your ISP. > Is this for real? I did a "replay all" to respond to your post. What are you suggesting? I don't see anything that looks like an obfuscated email. Jon -- http://mail.python.org/mailman/listinfo/python-list