Python: can __file__ be None if import succeeds?

I am working on Python 2.6 / 2.7 code that 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 , which is used to see if gmpy is installed on the system, and if not, do anything. However, I do not understand why the check is needed if gmpy.__file__ is None ; it seems redundant.

Are there any circumstances when trying to import a package, but the package path would be empty? Is this a double check of failure-free protection against a damaged installation?

+6
source share
2 answers

This check does not make sense. If the module / package was successfully imported, __file__ will never be anything, it will be the path to the module.

+2
source

The docs say, β€œThe __file__ attribute __file__ missing for C modules that are statically linked to the interpreter,” so I find it redundant. Besides that, what difference does the module object have.

+2
source

All Articles