Friday Filosofical Finking: Import protections

Grant Edwards grant.b.edwards at gmail.com
Wed Apr 17 16:44:53 EDT 2019


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.

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

> 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.

> 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.

> 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.

> 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. :)

-- 
Grant Edwards               grant.b.edwards        Yow! WHOA!!  Ken and Barbie
                                  at               are having TOO MUCH FUN!!
                              gmail.com            It must be the NEGATIVE
                                                   IONS!!




More information about the Python-list mailing list