You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyInstaller单文件打包exe报tables.libs缺失FileNotFoundError咨询

PyInstaller 5.1 单文件打包pytables找不到tables.libs报错解决

问题现象

Python 3.10环境下使用PyInstaller 5.1打包单文件应用,运行生成的exe时抛出如下报错:

Traceback (most recent call last):
  File "__main__.py", line 19, in <module>
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "tables\__init__.py", line 38, in <module>
  File "tables\__init__.py", line 22, in _delvewheel_init_patch_0_0_17
  File "os.py", line 1117, in add_dll_directory
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\\Users\\natas\\AppData\\Local\\Temp\\_MEI104162\\tables.libs'
[2216] Failed to execute script '__main__' due to unhandled exception!

已尝试两种方案均未生效:

  • 将tables.libs文件夹路径加入pathex配置,对应配置如下:
pathex = ['C:\\Users\\natas\\Documents\\SINDEL\\MDM_ver1\\mdm_ver1', 'C:\\Users\\natas\\Documents\\SINDEL\\MDM_ver1\\.venv\\Lib\\site-packages\\tables.libs']
  • 将tables.libs文件夹内所有dll逐一添加到binaries配置项,对应配置如下:
p = r'C:\Users\natas\Documents\SINDEL\MDM_ver1\.venv\Lib\site-packages\tables.libs'
added_binaries = [
    (os.path.join(p, 'blosc-965398da.dll'), '.'),
    (os.path.join(p, 'hdf5-a241c97c.dll'), '.'),
    (os.path.join(p, 'libbz2-e4ba6a29.dll'), '.'),
    (os.path.join(p, 'zlib-e2d94386.dll'), '.'),
    (os.path.join(p, 'zstd-34920894.dll'), '.')
]
a = Analysis(
    ['__main__.py'],
    pathex=['C:\\Users\\natas\\Documents\\SINDEL\\MDM_ver1\\mdm_ver1'],
    binaries=added_binaries,
    datas=[],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)

目前仅在单目录模式下,手动将tables.libs文件夹复制到程序根目录时,程序才能正常运行。原始使用的.spec文件内容如下:

# -*- mode: python ; coding: utf-8 -*-

import os

block_cipher = None

a = Analysis(
    ['__main__.py'],
    pathex=['C:\\Users\\natas\\Documents\\SINDEL\\MDM_ver1\\mdm_ver1'],
    binaries=[],
    datas=[],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a.datas,
    [],
    name='__main__',
    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,
)

故障原因

pytables(即导入的tables模块)通过delvewheel生成初始化补丁,启动时硬编码会在临时解压目录(_MEIxxxxxx)的根路径下查找 tables.libs 子文件夹,加载其中的dll依赖:

  • pathex配置仅用于告知PyInstaller搜索导入模块的路径,不会按原目录结构将tables.libs文件夹打包到临时目录对应位置
  • 将dll单独添加到binaries配置时,所有dll会被直接解压到临时目录根路径,不存在tables.libs子文件夹,依然匹配不到代码里写死的查找路径

修复方案

直接将整个tables.libs文件夹作为数据资源打包,指定解压后的目标路径为tables.libs,保证解压后的目录结构和delvewheel补丁的查找逻辑一致。修改后的.spec文件配置如下:

# -*- mode: python ; coding: utf-8 -*-
import os

block_cipher = None
# 替换为自身环境下tables.libs的实际绝对路径
tables_libs_path = r'C:\Users\natas\Documents\SINDEL\MDM_ver1\.venv\Lib\site-packages\tables.libs'

a = Analysis(
    ['__main__.py'],
    pathex=['C:\\Users\\natas\\Documents\\SINDEL\\MDM_ver1\\mdm_ver1'],
    binaries=[],
    # 把整个tables.libs文件夹打包,解压到临时目录根下的tables.libs路径
    datas=[(tables_libs_path, 'tables.libs')],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a.datas,
    [],
    name='__main__',
    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,
)

配置修改完成后,重新通过该spec文件执行打包即可,不需要额外复制dll或文件夹,单文件模式下可正常运行。


内容的提问来源于stack exchange,提问作者knv_777

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.27 06:18:24