9

在使用 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 上的一些问题的建议升级到 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.')

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

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

4

6 回答 6

6

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

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

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

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

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

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

您仍然需要返回原始目录才能使用 Git 并在不冻结的情况下运行您的.git代码redacted_folder


例如,我遇到了.gitand的问题redacted_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
于 2019-10-02T05:53:24.587 回答
1

这是固定的。更新 PyInstaller。

于 2021-06-16T23:03:27.730 回答
1

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

于 2021-04-10T11:19:02.253 回答
0

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

于 2021-03-28T10:44:40.397 回答
0

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

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

Microsoft Store 版本中的某些内容似乎很可能导致此类问题。

于 2020-04-28T18:08:56.500 回答
0

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

于 2021-09-19T12:40:21.217 回答