PyInstaller: “ModuleNotFoundError: No module named ‘typing_extensions'” Error in Standalone Executable

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!

New contributor

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật