Friday Filosofical Finking: Import protections
akkana at shallowsky.com
Thu Apr 18 12:44:49 EDT 2019
Chris Angelico writes:
> I write this as:
> import whois # ImportError? pip install python-whois
[ ... ]
> it means that normal exception handling is still
> happening (which might be important if I import this into something
> else), plus it's printing the message to stderr rather than stdout
[ ... ]
> About the only downside is that it assumes the .py file is available -
> this won't work with a .pyc-only setup.
It also assumes the user is capable of (1) finding the .py file
(maybe not so easy if it's imported from another program) and
(2) knowing Python well enough to find and understand the line with
the comment. Which in my example is not a problem since I'm probably
the only user of my domaincheck script; but when writing programs
for regular users, when you know an error is both likely and unclear
to read, it might make sense to catch the exception and print
a clearer message.
Your counterarguments are quite valid, though. It's a trade-off.
More information about the Python-list