如何将 .pyd 文件与 PyInstaller 捆绑在一起?

问题描述

我希望将我的脚本打包为可执行文件,以便我可以在未安装 Python 的计算机上运行它。

我正在使用 PyInstaller 为 my_program.py 创建一个可执行文件

我使用的是 OR-Tools (ortools) Python 包,具体来说,我使用的是 ortools.linear_solver.pywraplp 模块。

导入调用在我的代码中是这样进行的: from ortools.linear_solver import pywraplp

当我创建可执行文件并在我的计算机上运行它时,我收到此消息:

# ortools.linear_solver._pywraplp not found in PYZ
# swig_runtime_data4 not found in PYZ
# extension module 'ortools.linear_solver._pywraplp' loaded from 'C:\\Users\\<My Name>\\<My Path>\\project_files\\dist\\my_program\\ortools\\linear_solver\\_pywraplp.pyd'
# extension module 'ortools.linear_solver._pywraplp' executed from 'C:\\Users\\<My Name>\\<My Path>\\project_files\\dist\\my_program\\ortools\\linear_solver\\_pywraplp.pyd'
import 'ortools.linear_solver._pywraplp' # <_frozen_importlib_external.ExtensionFileLoader object at 0x000001A71756C640>

当我在 Windows 沙盒机器上运行它时,我收到此错误

Traceback (most recent call last):
  File "my_program.py",line 1,in <module>
  File "<frozen importlib._bootstrap>",line 991,in _find_and_load
  File "<frozen importlib._bootstrap>",line 975,in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>",line 671,in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py",line 531,in exec_module
  File "my_other_program.py",line 8,line 1042,in _handle_fromlist
  File "<frozen importlib._bootstrap>",line 219,in _call_with_frames_removed
  File "<frozen importlib._bootstrap>",in exec_module
  File "ortools\linear_solver\pywraplp.py",line 13,line 657,in _load_unlocked
  File "<frozen importlib._bootstrap>",line 556,in module_from_spec
  File "<frozen importlib._bootstrap_external>",line 1101,in create_module
  File "<frozen importlib._bootstrap>",in _call_with_frames_removed
ImportError: DLL load Failed while importing _pywraplp: The specified module Could not be found.

我的理解(我可能完全错了)是可执行文件没有找到 ortools.linear_solver._pywraplp 模块,必须使用 _pywraplp.pyd我的本地驱动器。但是,当我在沙盒机器上运行它时,没有地方获取 _pywraplp.pyd 文件,因此程序失败。

这是我用于创建可执行文件的规范文件

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

block_cipher = None


a = Analysis(['my_program.py'],pathex=['C:\\Users\\<My Name>\\<My Path>\\project_files'],binaries=[('C:\\Users\\<My Name>\\<My Path>\\project_files\\ortools\\linear_solver\\_pywraplp.pyd','ortools\\linear_solver\\')],datas=[('C:\\Users\\<My Name>\\<My Path>\\project_files\\ortools\\linear_solver\\_pywraplp.pyd',hiddenimports=['ortools.linear_solver.pywraplp','ortools.linear_solver._pywraplp'],hookspath=[],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,[('v',None,'OPTION')],exclude_binaries=True,name='my_program',debug=False,bootloader_ignore_signals=False,strip=False,upx=True,console=True,icon='graph_icon.ico')
coll = COLLECT(exe,a.binaries,a.zipfiles,a.datas,upx_exclude=[],name='my_program')

我认为解决此问题的方法是将 _pywraplp.pyd 文件打包到可执行文件中,但到目前为止我还没有任何运气。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)