Re: suppressing import errors

2011-11-16 Thread Andreea Babiuc
Loving the offtopic guys, sorry I have to go back to my problem now.. In the module I want to import I have a few import statements for Maya commands that don't work outside Maya unless I use the Maya standalone interpreter. So before I import this module I need to make sure I import maya and

suppressing import errors

2011-11-15 Thread Andreea Babiuc
Hi, Is there a way to suppress all the errors when importing a module in python? By that I mean.. If I have other imports in the module I'm trying to import that fail, I still want my module to be imported that way.. Many thanks. -- http://mail.python.org/mailman/listinfo/python-list

Re: suppressing import errors

2011-11-15 Thread Chris Kaynor
As with any Python code, you can wrap the import into a try: except block. try: import badModule except: pass # Or otherwise handle the exception - possibly importing an alternative module. As with any except statement, specific exceptions may be caught (rather than the blank, catch

Re: suppressing import errors

2011-11-15 Thread Andreea Babiuc
On 15 November 2011 17:24, Chris Kaynor ckay...@zindagigames.com wrote: As with any Python code, you can wrap the import into a try: except block. try: import badModule except: pass # Or otherwise handle the exception - possibly importing an alternative module. Hmm, I know this

Re: suppressing import errors

2011-11-15 Thread David Riley
On Nov 15, 2011, at 12:35 PM, Andreea Babiuc wrote: On 15 November 2011 17:24, Chris Kaynor ckay...@zindagigames.com wrote: As with any Python code, you can wrap the import into a try: except block. try: import badModule except: pass # Or otherwise handle the exception -

Re: suppressing import errors

2011-11-15 Thread Jean-Michel Pichavant
David Riley wrote: On Nov 15, 2011, at 12:35 PM, Andreea Babiuc wrote: On 15 November 2011 17:24, Chris Kaynor ckay...@zindagigames.com wrote: As with any Python code, you can wrap the import into a try: except block. try: import badModule except: pass # Or otherwise handle the

Re: suppressing import errors

2011-11-15 Thread David Riley
On Nov 15, 2011, at 1:58 PM, Jean-Michel Pichavant wrote: PS : @Dave there is a way to avoiding adding symbols to your global namespace, assign None to the module's name on import errors. Then before using it, just test the module bool value : if serial: serial.whateverMethod() True, and

Re: suppressing import errors

2011-11-15 Thread Chris Angelico
On Wed, Nov 16, 2011 at 6:39 AM, David Riley fraveyd...@gmail.com wrote: True, and that does avoid polluting namespace.  However, you shouldn't be testing for None as a bool; you should instead do an if module is None: (or, of course, is not None). Why not? Is there some other way for the

Re: suppressing import errors

2011-11-15 Thread David Riley
On Nov 15, 2011, at 3:01 PM, Chris Angelico wrote: On Wed, Nov 16, 2011 at 6:39 AM, David Riley fraveyd...@gmail.com wrote: True, and that does avoid polluting namespace. However, you shouldn't be testing for None as a bool; you should instead do an if module is None: (or, of course, is

Re: suppressing import errors

2011-11-15 Thread Chris Angelico
On Wed, Nov 16, 2011 at 8:20 AM, David Riley fraveyd...@gmail.com wrote:    Comparisons to singletons like None should always be done with      'is' or 'is not', never the equality operators.      Also, beware of writing if x when you really mean if x is not None      -- e.g. when testing

Re: suppressing import errors

2011-11-15 Thread Arnaud Delobelle
On 15 November 2011 21:34, Chris Angelico ros...@gmail.com wrote: On Wed, Nov 16, 2011 at 8:20 AM, David Riley fraveyd...@gmail.com wrote:      Comparisons to singletons like None should always be done with      'is' or 'is not', never the equality operators.      Also, beware of writing if x

Re: suppressing import errors

2011-11-15 Thread Ian Kelly
On Tue, Nov 15, 2011 at 2:42 PM, Arnaud Delobelle arno...@gmail.com wrote: It's idiomatic to write x is None when you want to know whether x is None. It's also idiomatic to just write if x: when you want to know whether x is something or nothing, and that's what I would probably do here. Either

Re: suppressing import errors

2011-11-15 Thread Chris Kaynor
On Tue, Nov 15, 2011 at 1:34 PM, Chris Angelico ros...@gmail.com wrote: On Wed, Nov 16, 2011 at 8:20 AM, David Riley fraveyd...@gmail.com wrote:      Comparisons to singletons like None should always be done with      'is' or 'is not', never the equality operators.      Also, beware of

Re: suppressing import errors

2011-11-15 Thread Steven D'Aprano
On Tue, 15 Nov 2011 14:22:21 -0800, Chris Kaynor wrote: The tests (the code is shown later - its about 53 lines, with lots of copy+paste...): Holy unnecessarily complicated code Batman! This is much simpler: [steve@ando ~]$ python -m timeit -s x = None if x is None: pass 1000 loops, best

Re: suppressing import errors

2011-11-15 Thread Alan Meyer
On 11/15/2011 4:20 PM, David Riley wrote: ... None was set to some other value. The other value might have a type (such as a container) that could be false in a boolean context! Obviously, that last bit doesn't apply to modules; they're not going to evaluate as False in general.

Re: suppressing import errors

2011-11-15 Thread David Riley
On Nov 15, 2011, at 5:59 PM, Alan Meyer wrote: On 11/15/2011 4:20 PM, David Riley wrote: ... None was set to some other value. The other value might have a type (such as a container) that could be false in a boolean context! Obviously, that last bit doesn't apply to modules;