I am working on Python 2.6/2.7 code which contains the following:
try:
import gmpy
gmpy_imported=True
except ImportError:
gmpy_imported=False
if gmpy_imported and gmpy.__file__ is None:
gmpy_imported=False
I can understand the try-except part, which is used to see if gmpy has been installed on the system — and if not, to do whatever. However, I do not understand why the if gmpy.__file__ is None check is necessary; it seems redundant.
Are there any circumstances when importing a package would appear to have succeeded, but the path to the package would in fact be empty? Is this double-check a failsafe against a corrupted installation?
There’s no point in this check. If the module/package had been successfully been imported,
__file__would never be none, it’d be the path of the module.