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 Andreea Babiuc
On 15 November 2011 17:24, Chris Kaynor  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 might sound silly, but if it fails I still want to import
the module and disable those lines of code that are related to the reason
while the module failed to be imported in the first place.  Even if that
makes the code not 100% correct.

Does that make sense ?









> As with any except statement, specific exceptions may be caught
> (rather than the blank, catch everything).
>
> Chris
>
> On Tue, Nov 15, 2011 at 9:11 AM, Andreea Babiuc 
> wrote:
> > 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
> >
> >
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Blog: Http://andreeababiuc.ro
Photo Portfolio: http://royaa.daportfolio.com
-- 
http://mail.python.org/mailman/listinfo/python-list


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
maya.standalone.
I make sure I place the correct paths in sys.path, but I get the following
error:
import maya.standalone
ImportError:
/apps/Linux64/aw/maya2012/lib/python2.6/site-packages/maya/../../../../lib/libOGSDeviceOGL-2_7.so:
undefined symbol: cgGetParameterBufferIndex
Now, I've googled this error and couldn't find anything on it and I'd have
no idea why it wouldn't work. It's not a python related error so I
understand if you couldn't help me with this, but since you've asked :D

I am thinking of using eval for each line in the module i want to import
(instead of importing it )  and just ignoring the maya related commands.
I can't and shouldn't edit any of these modules, I instead have to parse
them and interpret the parameters types without actually executing the
functions..

Thanks a lot,
Andreea





On 15 November 2011 18:58, Jean-Michel Pichavant wrote:

> David Riley wrote:
>
>> On Nov 15, 2011, at 12:35 PM, Andreea Babiuc wrote:
>>
>>
>>
>>> On 15 November 2011 17:24, Chris Kaynor 
>>> 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 might sound silly, but if it fails I still want to
>>> import the module and disable those lines of code that are related to the
>>> reason while the module failed to be imported in the first place.  Even if
>>> that makes the code not 100% correct.
>>>
>>> Does that make sense ?
>>>
>>>
>>
>> It makes sense.  It probably also makes sense to only do an "except
>> ImportError", since if there are other errors (say, syntax errors in a
>> module you're trying to import, rather than its absence, you probably want
>> to know about it.
>>
>> To disable code that won't work without the module you're trying to
>> import, you can always set flags in your module.  For example, I've got a
>> project at work that can use a variety of communications interfaces,
>> including using PySerial for serial port comms.  But if someone doesn't
>> have PySerial installed, I want it to fail gracefully and just not support
>> serial.  So I can do the following:
>>
>>
>> try:
>>   import serial
>>   _serial_enabled = True
>> except ImportError:
>>   print("PySerial not installed - serial ports not supported!")
>>   _serial_enabled = False
>>
>>
>> And then elsewhere in my module, I can check the value of _serial_enabled
>> to see if I should e.g. list the serial ports in available communications
>> interfaces.
>>
>> Of course, if there's some other error in PySerial (maybe I installed a
>> broken version with a syntax error?), that error will get propagated up,
>> which is a good thing, because I'd rather know that PySerial is broken than
>> just have it tell me it's not installed (which is what would happen if I
>> simply caught all exceptions).  Your mileage may vary.
>>
>> - Dave
>>
>>
>>
> If I'm not wrong the OP wants to disable the line *in the module being
> imported*, which is kindof silly and doesn't make sense to answer his
> question.
>
> Anreea, tell us why the module you are importing is failing and if this
> module is yours, we may provide you a proper way to handle this situation
> (though I'm pretty sure everything is in Dave's proposal).
>
> JM
> 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()
>



-- 
Blog: Http://andreeababiuc.ro
Photo Portfolio: http://royaa.daportfolio.com
-- 
http://mail.python.org/mailman/listinfo/python-list