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

标签 anaconda pyinstaller environment pywin32

在使用 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
  • Anaconda 3,Windows
  • 使用 PyInstaller 编译 --onedir

  • 我试过的:
  • 尝试按照此处的建议将 PyInstaller 降级到 2.1:https://github.com/pyinstaller/pyinstaller/issues/3916但由于不支持 Python 3 而无法使用
  • 最初在 PyInstaller 3.5 上,按照 PyInstaller github
  • 上的一些问题的建议升级到 dev 版本
  • 尝试包括 --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'
        ])
    

    完整的堆栈跟踪:
    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.')
    

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

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

    最佳答案

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

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

  • 在卡住前将文件夹移出目录
  • 此外,如果运行卡住的应用程序需要该文件夹(例如,作为数据文件夹),我也会:
  • 评论/删除 --add-data 的对应部分并手动复制文件夹,或
  • 更改 --add-data 的路径例如如果您搬家 redacted_folder到您的桌面,您可以更改为 '--add-data=C:/Users/User1/Desktop/redacted_folder'

  • 您仍然需要 .gitredacted_folder返回原始目录以使用 Git 并在不卡住的情况下运行您的代码

    例如,我遇到了 .git 的问题。和 redacted_folder .需要卡住的可执行文件redacted_folder运行部分代码。

    我所做的是:
  • 移动.gitredacted_folder到一个单独的目录
  • 然后,我注释掉了为 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'
        ])
    
  • 卡住后,我复制了redacted_folder移动到相对于 distpath 中卡住的可执行文件的适当位置
  • 关于anaconda - PyInstaller win32ctypes.pywin32.pywintypes.error : (2, 'LoadLibraryExW' , 'The system cannot find the file specified.'),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57932432/

    相关文章:

    anaconda - 使用 conda install 时更改超时限制

    python - 为什么要在 conda 环境中添加一个 channel ?

    python-requests - python请求转换为.exe时找不到带有证书的文件夹

    python - 将 kivy 应用程序打包成一个 exe

    powershell - 从文本文件读取变量并将其设置为当前 PowerShell session 环境变量

    c# - 如何在 C# 中运行一系列进程并保留其环境设置?

    必须使用 native 代码时的 Java 工作环境

    python - 比较两个 anaconda 安装之间的包

    python - 使用 scikit-tensor 进行张量分析

    python - Pyinstaller 可执行文件无法导入 torchvision