PyInstaller win32ctypes.pywin32.pywintypes.error: (2, 'LoadLibraryExW', '系统找不到指定的文件。')

per*_*tio 8 environment pywin32 pyinstaller anaconda

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

  • Python版本:3.7
  • 图形用户界面:PySide2 5.13.0
  • 其他包:lxml、BeautifulSoup、Matplotlib(依赖于 numpy)、Pandas、pypiwin32、reportlab、Theano
  • 蟒蛇 3, 视窗
  • 使用 PyInstaller --onedir 编译

我试过的:

  • 尝试按照此处的建议将 PyInstaller 降级到 2.1:https : //github.com/pyinstaller/pyinstaller/issues/3916但不起作用,因为不支持 Python 3
  • 最初是在 PyInstaller 3.5 上,按照 PyInstaller github 上的一些问题的建议升级到开发版本
  • 尝试包括 --exclude-module=.git
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'
    ])
Run Code Online (Sandbox Code Playgroud)

完整的堆栈跟踪:

79702 DEBUG: Analyzing .git\objects\78\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.')
Run Code Online (Sandbox Code Playgroud)

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

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

per*_*tio 5

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

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

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

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

    • 注释/删除相应部分--add-data并手动复制文件夹,或

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

您仍然需要.gitredacted_folder回到原来的目录使用Git和不冻结运行代码


例如,我在使用.gitand 时遇到了麻烦redacted_folderredacted_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'
    ])
Run Code Online (Sandbox Code Playgroud)
  1. 冻结后,我复制redacted_folder到与冻结的可执行文件相关的适当位置distpath