Friday Filosofical Finking: Import protections

DL Neil PythonList at DancesWithMice.info
Thu Apr 18 00:05:59 EDT 2019


On 18/04/19 8:45 AM, MRAB wrote:
> On 2019-04-17 21:20, DL Neil wrote:
>> Do you bother with exception handling for import statements?
>>
>>
>> Can we assume that if such a catastrophic error occurs, it is quite
>> acceptable for the code to fall-over in a tumbling-fumble?

> [snip]
> Catch only what you (well, the script) can fix.
> 
> If it needs numpy, but can't import numpy, then when can it do? Might as 
> well just let it fail.
> 
> I suppose an alternative might be to try to download and install numpy 
> and then retry, but what if it can't be downloaded, or the installation 
> fails?
> 
> No, you have to give up at some point. It's better just to report the 
> problem and leave it to the user to fix it.

Ah, but is that not the point - the user cannot fix it (and neither 
should he be allowed to do so: see later comments about Operations 
functions).

What the user faces (unless we more properly handle the exception) is:

import davids_brains
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'davids_brains'

You and I will think this fine - and 'here' on the list will even beg 
OPs to give us this much data.

However, despite users who have been known to make other comments about 
my (?lack of) brains, they will tend to offer such helpful advice as: 
"we need the missing module"...!

-- 
Regards =dn



More information about the Python-list mailing list