Friday Filosofical Finking: Import protections

DL Neil PythonList at DancesWithMice.info
Thu Apr 18 00:21:10 EDT 2019


On 18/04/19 8:44 AM, Grant Edwards wrote:
> On 2019-04-17, DL Neil <PythonList at DancesWithMice.info> wrote:
> 
>> Do you bother with exception handling for import statements?
> 
> Sometimes.  There are two cases when I do that:
> 
>   1. When the module has different names under Python2 and Python3 and
>      the program tries first one, then the other.

Excellent example - and a lot easier than interrogating os.environ (for 
example), ie permission cf forgiveness.


>   2. When the program can still do something useful (if perhaps
>      feature-limited) without the imported module by substituting
>      something else in its place.

Any (publishable) examples?


>> Most of the code I read, both in books and during code review,
>> eschews any form of ImportError check. Even data science people who
>> 'clean' every data field towards inclusion/exclusion in the
>> analysis, take for granted that numpy, scipy, pandas, et al, will be
>> available to their code.
> 
> You've omitted the second thing assumed by the authors: without numpy,
> scipy, pandas, et alia the program can do nothing useful.

but... what of the third inherent assumption: that the user(s) will be 
able to handle the situation (discussed in another msg 'here')?


>> Does such a check seem un-pythonic? [sto] (maybe 'forgiveness cf
>> permission'?)
> 
> It's probably rather unpythonic if you're not going to anything useful
> in the exception handler.

Indisputable!


>> Can we assume that if such a catastrophic error occurs, it is quite
>> acceptable for the code to fall-over in a tumbling-fumble?
> 
> It's certainly OK with me.  I'm not sure why you refer to raising an
> exception as "fall-over in a tumbling fumble".  Raising an exception
> is the normal way to indicate failure in Python.

Apologies! Evidently a cultural reference that did not export well.

To understand "tumbling fumble" perhaps think of dropping something, 
attempting to catch it with one hand, then having to make a second try 
with the other, and probably failing to intercede before the floor... I 
could have used other terms, but likely to be considered 
'politically-INcorrect'.

No, "tumbling fumble" describes having no exception handling and merely 
allowing the program to "crash".

Thus the basic question: why do we (apparently) so seldom consider the 
possibility of an ImportError?


>> Does it make a difference if operating in/out of a dev-ops
>> environment?
> 
> I have no idea what "a dev-ops environment means", and I plan on
> keeping it that way. :)

I see that you value your sanity. On the other hand you don't seem so 
worried about your physical safety - please note that I'll be hiding 
behind you, should the (ever-so nice) dev-ops lady on one of my projects 
happen to read this...

-- 
Regards =dn


More information about the Python-list mailing list