Friday Filosofical Finking: Import protections

DL Neil PythonList at DancesWithMice.info
Wed Apr 17 16:20:03 EDT 2019


(I know it's not Friday [exp], and after personal apologies[apo])


Do you bother with exception handling for import statements?


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.


Does such a check seem un-pythonic? [sto] (maybe 'forgiveness cf 
permission'?)

Can we assume that if such a catastrophic error occurs, it is quite 
acceptable for the code to fall-over in a tumbling-fumble?

Does it make a difference if operating in/out of a dev-ops environment?

Might such only occur once, because once the environment has been 
correctly-built, it will/can *never* happen again?

Can we assume Built-in and PSL modules *must* be present, so we only 
need to talk about covering in-house code and pip-ed (or similar) modules?

Is it more/less important in __main__ than in an imported module?

Do you handle each import separately, or all in a single try..except block?

Do you try to anticipate and cover every import in the system at the top 
of __main__, eg imports inside imported modules?

What about OpSys-specific imports (etc)?

Do modules import-ed only in specific situations, deserve more, or less, 
attention?


Refs:

[apo] Change of season coughs and sniffles, duly shared with the CO (oops!)

[exp] Explanation: in the Christian calendar, this Friday is "Good 
Friday" and thus a national/public holiday in much of the western world. 
Thus, also next ("Easter") Monday and possibly Tuesday. To continue the 
theme: next Thursday is also a holiday in New Zealand and Australia 
("ANZAC Day": equating to Veterans' Day, Memorial Day, Independence Day, 
etc)

[imp] 5. The import system https://docs.python.org/3.6/reference/import.html

[sto] A somewhat similar question: 
https://stackoverflow.com/questions/3131217/error-handling-when-importing-modules

-- 
Regards,
=dn


More information about the Python-list mailing list