ERROR: Failed to import module __PyInstaller_hooks_0_narwhals required by hook for module hook-narwhals.py.
buggood first issue
**Describe the bug**
* Which hook/library isn't working?
narwhals
* Does the error get raised while building or when running?
It get raised while building.
**To Reproduce**
A minimal example file(test.py):
```
import narwhals
```
PyInstaller command:
```
pyinstaller test.py
```
Error:
```
Traceback (most recent call last):
File "D:\work\python_work\test\venv\Lib\site-packages\PyInstaller\depend\imphook.py", line 422, in _load_hook_module
self._hook_module = importlib_load_source(self.hook_module_name, self.hook_filename)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\work\python_work\test\venv\Lib\site-packages\PyInstaller\compat.py", line 615, in importlib_load_source
mod_loader.exec_module(mod)
File "<frozen importlib._bootstrap_external>", line 940, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "D:\work\python_work\test\venv\Lib\site-packages\_pyinstaller_hooks_contrib\stdhooks\hook-narwhals.py", line 18, in <module>
datas += copy_metadata("typing_extensions")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\work\python_work\test\venv\Lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 969, in copy_metadata
dist = importlib_metadata.distribution(package_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\DELL\AppData\Local\Programs\Python\Python311\Lib\importlib\metadata\__init__.py", line 964, in distribution
return Distribution.from_name(distribution_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\DELL\AppData\Local\Programs\Python\Python311\Lib\importlib\metadata\__init__.py", line 561, in from_name
raise PackageNotFoundError(name)
importlib.metadata.PackageNotFoundError: No package metadata was found for typing_extensions
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "D:\work\python_work\test\venv\Scripts\pyinstaller.exe\__main__.py", line 7, in <module>
File "D:\work\python_work\test\venv\Lib\site-packages\PyInstaller\__main__.py", line 231, in _console_script_run
run()
File "D:\work\python_work\test\venv\Lib\site-packages\PyInstaller\__main__.py", line 215, in run
run_build(pyi_config, spec_file, **vars(args))
File "D:\work\python_work\test\venv\Lib\site-packages\PyInstaller\__main__.py", line 70, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
File "D:\work\python_work\test\venv\Lib\site-packages\PyInstaller\building\build_main.py", line 1270, in main
build(specfile, distpath, workpath, clean_build)
File "D:\work\python_work\test\venv\Lib\site-packages\PyInstaller\building\build_main.py", line 1208, in build
exec(code, spec_namespace)
File "D:\work\python_work\test\test.spec", line 4, in <module>
a = Analysis(
^^^^^^^^^
File "D:\work\python_work\test\venv\Lib\site-packages\PyInstaller\building\build_main.py", line 582, in __init__
self.__postinit__()
File "D:\work\python_work\test\venv\Lib\site-packages\PyInstaller\building\datastruct.py", line 184, in __postinit__
self.assemble()
File "D:\work\python_work\test\venv\Lib\site-packages\PyInstaller\building\build_main.py", line 712, in assemble
program_scripts.append(self.graph.add_script(script))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\work\python_work\test\venv\Lib\site-packages\PyInstaller\depend\analysis.py", line 283, in add_script
self._top_script_node = super().add_script(pathname)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\work\python_work\test\venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1177, in add_script
self._process_imports(n)
File "D:\work\python_work\test\venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2629, in _process_imports
target_modules = self._safe_import_hook(*import_info, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\work\python_work\test\venv\Lib\site-packages\PyInstaller\depend\analysis.py", line 454, in _safe_import_hook ret_modules = super()._safe_import_hook(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\work\python_work\test\venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2086, in _safe_import_hook
target_modules = self.import_hook(
^^^^^^^^^^^^^^^^^
File "D:\work\python_work\test\venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1249, in import_hook
target_package, target_module_partname = self._find_head_package(
^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\work\python_work\test\venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1428, in _find_head_package
target_package = self._safe_import_module(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\work\python_work\test\venv\Lib\site-packages\PyInstaller\depend\analysis.py", line 516, in _safe_import_module
return super()._safe_import_module(module_basename, module_name, parent_package)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\work\python_work\test\venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1809, in _safe_import_module
self._process_imports(n)
File "D:\work\python_work\test\venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2629, in _process_imports
target_modules = self._safe_import_hook(*import_info, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\work\python_work\test\venv\Lib\site-packages\PyInstaller\depend\analysis.py", line 385, in _safe_import_hook excluded_imports = self._find_all_excluded_imports(source_module.identifier)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\work\python_work\test\venv\Lib\site-packages\PyInstaller\depend\analysis.py", line 373, in _find_all_excluded_imports
excluded_imports.update(module_hook.excludedimports)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\work\python_work\test\venv\Lib\site-packages\PyInstaller\depend\imphook.py", line 343, in __getattr__
self._load_hook_module()
File "D:\work\python_work\test\venv\Lib\site-packages\PyInstaller\depend\imphook.py", line 425, in _load_hook_module
raise ImportErrorWhenRunningHook(self.hook_module_name, self.hook_filename)
PyInstaller.exceptions.ImportErrorWhenRunningHook: ERROR: Failed to import module __PyInstaller_hooks_0_narwhals required by hook for module D:\work\python_work\test\venv\Lib\site-packages\_pyinstaller_hooks_contrib\stdhooks\hook-narwhals.py. Please check whether module __PyInstaller_hooks_0_narwhals actually exists and whether the hook is compatible with your version of D:\work\python_work\test\venv\Lib\site-packages\_pyinstaller_hooks_contrib\stdhooks\hook-narwhals.py:
You might want to read more about hooks in the manual and provide a pull-request to improve PyInstaller.
```
**Expected behavior**
Build successfully
**Desktop (please complete the following information):**
- OS: Windows
- Python Version: 3.11.0
- Version of `pyinstaller-hooks-contrib`: 2025.3
- Version of PyInstaller: 6.13.0
- Version of Narwhals: 1.37.1
**Additional context**
It worked when the version of `pyinstaller-hooks-contrib` was 2025.2
[test.zip](https://github.com/user-attachments/files/20023477/test.zip)
关闭于 2025-05-03 2 条评论