Description:
I’m attempting to package a Python application into a standalone executable using PyInstaller. The application relies on several libraries, including PyTorch. Despite including typing_extensions in the hidden imports, I still encounter a ModuleNotFoundError for typing_extensions when running the executable on the client system.
Environment:
OS: Windows 11
Python Version: 3.12.3
PyInstaller Version: 6.6.0
Virtual Environment: Yes (using .venv)
Directory Structure:
project-root/
├── GUI4.py
├── hook-torch.py
├── gui4.spec
└── .venv/
└── Lib/
└── site-packages/
└── (all installed packages including torch and typing_extensions)
Issue:
When I run the generated executable on a client system, I get the following error:
Error: Traceback (most recent call last):
type here
File “C:PathtoExecutableNewExecutableNameNewExecutableName_internaldetect.py”, line 38, in
import torch
File “C:PathtoExecutableNewExecutableNameNewExecutableName_internaltorch_init_.py”, line 1403, in
from .serialization import save, load
File “C:PathtoExecutableNewExecutableNameNewExecutableName_internaltorchserialization.py”, line 18, in
from typing_extensions import TypeAlias # Python 3.10+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named ‘typing_extensions’
Spec File:
Here is my gui4.spec file:
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(
['GUI4.py'],
pathex=['.'],
binaries=[],
datas=[
('weights/best.pt', 'weights'),
('detect.py', '.'),
('models', 'models'),
('utils', 'utils'),
('data', 'data'),
('export.py', '.'),
('requirements.txt', '.'),
],
hiddenimports=[
'models.common',
'utils.general',
'utils.plots',
'export',
'typing_extensions',
'torch',
'torch.utils.tensorboard',
],
hookspath=['.', 'Path\to\your\project\.venv\Lib\site-packages'],
runtime_hooks=[],
excludes=[
'torch.distributed._shard.checkpoint._dedup_tensors',
'torch.distributed._shard.checkpoint._fsspec_filesystem',
'torch.distributed._shard.checkpoint._nested_dict',
'torch.distributed._shard.checkpoint._sharded_tensor_utils',
'torch.distributed._shard.checkpoint._state_dict_utils',
'torch.distributed._shard.checkpoint._traverse',
'torch.distributed._shard.checkpoint.api',
'torch.distributed._shard.checkpoint.default_planner',
'torch.distributed._shard.checkpoint.filesystem',
'torch.distributed._shard.checkpoint.metadata',
'torch.distributed._shard.checkpoint.optimizer',
'torch.distributed._shard.checkpoint.planner',
'torch.distributed._shard.checkpoint.planner_helpers',
'torch.distributed._shard.checkpoint.resharding',
'torch.distributed._shard.checkpoint.state_dict',
'torch.distributed._shard.checkpoint.state_dict_loader',
'torch.distributed._shard.checkpoint.state_dict_saver',
'torch.distributed._shard.checkpoint.stateful',
'torch.distributed._shard.checkpoint.storage',
'torch.distributed._shard.checkpoint.utils',
'torch.distributed._sharded_tensor._ops',
'torch.distributed._sharded_tensor._ops._common',
'torch.distributed._sharded_tensor._ops.binary_cmp',
'torch.distributed._sharded_tensor._ops.init',
'torch.distributed._sharded_tensor._ops.misc_ops',
'torch.distributed._sharded_tensor._ops.tensor_ops',
'torch.distributed._sharded_tensor.api',
'torch.distributed._sharded_tensor.logger',
'torch.distributed._sharded_tensor.logging_handlers',
'torch.distributed._sharded_tensor.metadata',
'torch.distributed._sharded_tensor.reshard',
'torch.distributed._sharded_tensor.shard',
'torch.distributed._sharded_tensor.utils',
'torch.distributed._sharding_spec._internals',
'torch.distributed._sharding_spec.api',
'torch.distributed._sharding_spec.chunk_sharding_spec',
'torch.distributed._sharding_spec.chunk_sharding_spec_ops',
'torch.distributed._sharding_spec.chunk_sharding_spec_ops._common',
'torch.distributed._sharding_spec.chunk_sharding_spec_ops.embedding',
'torch.distributed._sharding_spec.chunk_sharding_spec_ops.embedding_bag',
],
noarchive=False,
optimize=0,
cipher=block_cipher,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
exe = EXE(
pyz,
a.scripts,
[],
exclude_binaries=True,
name='GUI4',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)
coll = COLLECT(
exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='GUI4',
)
Custom Hook:
I also created a custom hook for PyTorch to ensure all dependencies are included:
hook-torch.py:
from PyInstaller.utils.hooks import collect_all
datas, binaries, hiddenimports = collect_all('torch')
typing_extensions_datas, typing_extensions_binaries, typing_extensions_hiddenimports = collect_all('typing_extensions')
datas += typing_extensions_datas
binaries += typing_extensions_binaries
hiddenimports += typing_extensions_hiddenimports
binaries = binaries
datas = datas
hiddenimports = hiddenimports
Steps Taken:
Installed all dependencies in a virtual environment.
Created a spec file and included necessary data files and hidden imports.
Added the site-packages directory to the hookspath in the spec file.
Created a custom hook to ensure PyTorch and typing_extensions are included.
Ran PyInstaller with the spec file to generate the executable.
Issue:
Despite these efforts, the executable still fails with a ModuleNotFoundError for typing_extensions on the client system.
Request:
I need help identifying why typing_extensions is not being included in the standalone executable and how to resolve this issue.
Any insights or suggestions would be greatly appreciated!
Skywalker_luke_00 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.