PyInstaller win32ctypes.pywin32.pywintypes.error: (2, 'LoadLibraryExW', 'The system cannot find the file specified.')

PyInstaller win32ctypes.pywin32.pywintypes.error: (2, 'LoadLibraryExW', 'The system cannot find the file specified.')

在使用 PyInstaller(开发版本 4.0.dev0+8196c57ab)时,生成了一个 OSError: [WinError 2] 'The system cannot find the file specified.'

我尝试过的:

import PyInstaller.__main__

if __name__ == '__main__':
    PyInstaller.__main__.run([
        '--name=%s' % 'Dummy App',
        '--onedir',
        '--nowindowed',
        r'--workpath=C:\Users\User1\Desktop\build7',
        r'--distpath=C:\Users\User1\Desktop\dist7',
        '--hidden-import=theano.tensor.shared_randomstreams',
        '--hidden-import=pandas._libs.tslibs.timedeltas',
        '--clean',
        '--add-data={0};.'.format('redacted.xml'),
        '--add-data={0};{0}'.format('redacted_folder'),
        '--add-data={0};.'.format('redacted.pdf'),
        '--exclude-module={0}'.format('.git'),
        '--log-level=WARN',
        'MainWindow.py'
    ])

完整堆栈跟踪:

79702 DEBUG: Analyzing .git\objects\e83411cea88cd038acb12c005a984fc0d6d423
Traceback (most recent call last):
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\win32ctypes\pywin32\pywintypes.py", line 35, in pywin32error
    yield
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\win32ctypes\pywin32\win32api.py", line 43, in LoadLibraryEx
    return _dll._LoadLibraryEx(fileName, 0, flags)
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\win32ctypes\core\ctypes\_util.py", line 42, in check_null
    raise make_error(function, function_name)
OSError: [WinError 2] The system cannot find the file specified.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/User1/Dropbox/GitHub_Repos/DiabetesReportGenerator_v2/pyinstaller_freeze.py", line 37, in <module>
    'MainWindow.py'
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\__main__.py", line 112, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\__main__.py", line 63, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\building\build_main.py", line 732, in main
    build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\building\build_main.py", line 679, in build
    exec(code, spec_namespace)
  File "C:\Users\User1\Dropbox\GitHub_Repos\DiabetesReportGenerator_v2\Risk Calculator.spec", line 17, in <module>
    noarchive=False)
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\building\build_main.py", line 242, in __init__
    self.__postinit__()
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\building\datastruct.py", line 158, in __postinit__
    self.assemble()
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\building\build_main.py", line 468, in assemble
    redirects=self.binding_redirects))
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\depend\bindepend.py", line 226, in Dependencies
    for ftocnm, fn in getAssemblyFiles(pth, manifest, redirects):
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\depend\bindepend.py", line 402, in getAssemblyFiles
    for assembly in getAssemblies(pth):
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\depend\bindepend.py", line 353, in getAssemblies
    res = GetManifestResources(pth)
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\utils\win32\winmanifest.py", line 1005, in GetManifestResources
    return winresource.GetResources(filename, [RT_MANIFEST], names, languages)
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\utils\win32\winresource.py", line 168, in GetResources
    hsrc = win32api.LoadLibraryEx(filename, 0, LOAD_LIBRARY_AS_DATAFILE)
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\win32ctypes\pywin32\win32api.py", line 43, in LoadLibraryEx
    return _dll._LoadLibraryEx(fileName, 0, flags)
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\contextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\win32ctypes\pywin32\pywintypes.py", line 37, in pywin32error
    raise error(exception.winerror, exception.function, exception.strerror)
win32ctypes.pywin32.pywintypes.error: (2, 'LoadLibraryExW', 'The system cannot find the file specified.')

P.S。编译和 exe 与之前从 Anaconda 环境创建的 virtualenv 环境完美运行(不需要 --exclude-module=.git),但包过于臃肿,因为 virtualenv 环境仍然被认为是 conda 环境的一部分。于是我换了个conda环境,就出现了这个错误

我已经通过在这个新的 conda 环境中创建另一个 virtualenv 环境来让它工作,但这是一种非常迂回的方式。有什么解决方法吗?

对我来说,PyInstaller 似乎不必要地搜索 .git 文件夹和 redacted_folder

我通过以下方式解决了这个问题:

  1. 在冻结之前将文件夹移出目录

  2. 此外,如果冻结的应用程序需要文件夹 运行(例如作为数据文件夹),我也会:

    • 评论/删除--add-data对应的部分,手动复制文件夹过来,或者

    • 更改 --add-data 的路径,例如如果您将 redacted_folder 移动到您的桌面,您可以更改为 '--add-data=C:/Users/User1/Desktop/redacted_folder'

您仍然需要 .gitredacted_folder 返回原始目录才能使用 Git 和 运行 您的代码而不冻结


例如,我在 .gitredacted_folder 上遇到了麻烦。冻结的可执行文件需要 redacted_folder 到 运行 部分代码。

我所做的是:

  1. .gitredacted_folder 移动到单独的目录

  2. 然后,我注释掉了为 redacted_folder 添加数据的行:

import PyInstaller.__main__

if __name__ == '__main__':
    PyInstaller.__main__.run([
        '--name=%s' % 'Dummy App',
        '--onedir',
        '--nowindowed',
        r'--workpath=C:\Users\User1\Desktop\build7',
        r'--distpath=C:\Users\User1\Desktop\dist7',
        '--hidden-import=theano.tensor.shared_randomstreams',
        '--hidden-import=pandas._libs.tslibs.timedeltas',
        '--clean',
        '--add-data={0};.'.format('redacted.xml'),
        # Remove `redacted_folder` and comment out the following line
        # '--add-data={0};{0}'.format('redacted_folder'),
        '--add-data={0};.'.format('redacted.pdf'),
        '--exclude-module={0}'.format('.git'),
        '--log-level=WARN',
        'MainWindow.py'
    ])
  1. 冻结后,我将 redacted_folder 复制到相对于 distpath
  2. 中冻结的可执行文件的适当位置

我遇到了同样的问题并找到了另一个线程,其中有人卸载了 Python 然后重新安装了 Python 来修复它。

密钥 了解问题的信息是,他们 最初 使用 Microsoft Store 安装 Python。当他们重新安装 Python 时,他们只是从 python.org 下载了 Python,这解决了问题。它对我也有用。

很可能是 Microsoft Store 版本中的某些东西导致了这样的问题。

从管理员 cmd 或 ide 开始,然后再试一次,它对我有帮助。

我使用 pip 在新的 conda 环境中安装了 pyinstaller,但是默认情况下没有安装 pywin32(尽管文档提到它会)。安装它为我修复了错误。

这是 fixed。更新 PyInstaller。

运行 来自具有提升权限的 Jupyter Notebook 对我有用!