brettlangdon / importhook

Python package for executing functions when packages are imported
https://brettlangdon.github.io/importhook/
MIT License
25 stars 4 forks source link

Don't wrap spec.loader in HookLoader if spec.loader is None #7

Open rkargon opened 1 year ago

rkargon commented 1 year ago

c.f. issue #6: https://github.com/brettlangdon/importhook/issues/6 for more details/examples

Certain packages that are 'namespace packages' don't return a loader object when they're, well, loaded. However in that case importhook still wraps spec.loader with a HookLoader(loader=None), which messes up Python/importlib's None checks so it tries to call spec.loader.load_module instead of skipping.

I was able to reproduce this on python3.10 with the following code:

import importhook
import snowflake # Causes attributeerror

The snowflake-related packages i have are:

snowflake-connector-python               2.9.0
snowflake-sqlalchemy                     1.4.6

I'm not too familiar with the internals of how modules are loaded by Python so it's possible there's a more elegant way around this, or something I'm missing. However this PR seems to solve the issue

rumeshkrish commented 5 months ago

Can someone(@brettlangdon) merge this change and release new version of lib ?

bitterpanda63 commented 2 months ago

Confirmed that this also works for me, would indeed be nice to see a new release