Open kevinushey opened 4 years ago
I just ran into the exact same problem -- before I start diving into this -- anybody else working on this?
Another instance, here with IPython
trying to import a magically-available method from pygments.lexers
, presumably due to some of this cleverness:
File "/lib/python3.10/asyncio/futures.py", line 201, in result
raise self._exception
File "/lib/python3.10/asyncio/tasks.py", line 232, in __step
result = coro.send(None)
File "/lib/python3.10/_pyodide/_base.py", line 531, in eval_code_async
await CodeRunner(
File "/lib/python3.10/_pyodide/_base.py", line 359, in run_async
await coroutine
File "<exec>", line 10, in <module>
File "/lib/python3.10/site-packages/pyolite/__init__.py", line 17, in <module>
from .display import LiteStream
File "/lib/python3.10/site-packages/importhook/loader.py", line 85, in exec_module
mod = self.loader.exec_module(module, *args, **kwargs)
File "/lib/python3.10/site-packages/pyolite/display.py", line 4, in <module>
from IPython.core.displayhook import DisplayHook
File "/lib/python3.10/site-packages/importhook/loader.py", line 85, in exec_module
mod = self.loader.exec_module(module, *args, **kwargs)
File "/lib/python3.10/site-packages/IPython/__init__.py", line 52, in <module>
from .terminal.embed import embed
File "/lib/python3.10/site-packages/importhook/loader.py", line 85, in exec_module
mod = self.loader.exec_module(module, *args, **kwargs)
File "/lib/python3.10/site-packages/IPython/terminal/embed.py", line 14, in <module>
from IPython.core.magic import Magics, magics_class, line_magic
File "/lib/python3.10/site-packages/importhook/loader.py", line 85, in exec_module
mod = self.loader.exec_module(module, *args, **kwargs)
File "/lib/python3.10/site-packages/IPython/core/magic.py", line 20, in <module>
from . import oinspect
File "/lib/python3.10/site-packages/importhook/loader.py", line 85, in exec_module
mod = self.loader.exec_module(module, *args, **kwargs)
File "/lib/python3.10/site-packages/IPython/core/oinspect.py", line 45, in <module>
from pygments.lexers import PythonLexer
ImportError: cannot import name 'PythonLexer' from 'pygments.lexers' (/lib/python3.10/site-packages/pygments/lexers/__init__.py)
Yes, this issue is indeed because pygments.lexers relies on it being able to change sys.modules, This line of code generates the error. Currently working on a fix, might just be not changing sys.modules if we don't modify the module? Testing this out on a larger repo to see if this is indeed the fix.
For those interested I made a fix here: https://github.com/AikidoSec/firewall-python/pull/247, Since this project is not actively maintianed we keep our own version of importhook. But yeah I don't see a reason to create a PR right now
First off -- thank you for
importhook
! It fills what feels like a surprising gap in Python's module import machinery.Unfortunately, I'm seeing that some modules may fail to load when an
importhook
hook is registered. For example, here's a Bash script to reproduce when attempting to loadtorch
:When I run this, I see:
It looks like the way that
importhook
patches the module finders + loaders interferes withtorch
in some odd way. Any idea what might be causing this?