py 项目有效,但 pyinstaller 给出与纸浆相关的错误

py project works but pyinstaller gives error related to pulp

我正在从事一个用纸浆进行线性优化的项目。当我通常 "run" 文件时,它运行良好,但当我尝试使用 pyinstaller 将其转换为 .exe 文件时,我遇到了一些问题。 如果我 运行 来自命令提示符的 .exe 文件报告错误:

Traceback (most recent call last):
  File "price.py", line 116, in <module>
  File "site-packages\pulp\pulp.py", line 1713, in solve
AttributeError: 'NoneType' object has no attribute 'actualSolve'
[9468] Failed to execute script price

我遇到问题的代码部分:

#type of problem
group_division = pulp.LpProblem("Group_division", pulp.LpMinimize)

#objective function to minimize
group_division += objectiveFunction(external_groups, internal_groups)

#specify the maximum number of groups
group_division += sum([x[group] for group in external_groups]) <= max_groups,                             
"Maximum_number_of_groups"

#every thickness must appear once 
for cutType in cutTypes:
group_division += oneCutConstraint(x, y, external_groups, internal_groups, cutType) == 
1,"Must_appear_%s"%cutType

#solve the problem    
group_division.solve()

当您使用来自 python 来源的纸浆时,解算器可用,因为该解算器是纸浆附带的唯一解算器。但是当你 运行 pyinstaller 时,它不再可用(因为你没有明确告诉 pyinstaller 复制它)。

用pyinstaller打包pulse时,你需要告诉pyinstaller去获取pulp自带的CBC求解器的目录。如果没有,您将只会获得 python 代码,然后您的打包版本将找不到 CBC 求解器。

假设您有一个用于 pyinstaller 的 config.spec 文件,您必须将其编辑为如下内容(至少这样的内容对我有用):

import sys
import os

block_cipher = None

def get_pulp_path():
    import pulp
    return pulp.__path__[0]

path_main = os.path.dirname(os.path.abspath(sys.argv[2]))

a = Analysis(['MAIN_SCRIPT.py'],
             pathex=[path_main],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)

a.datas += Tree(get_pulp_path(), prefix='pulp', excludes=["*.pyc"])

pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(pyz, a.scripts, exclude_binaries=True, name='NAME', debug=False, strip=False, upx=True, console=True)
coll = COLLECT(exe, a.binaries, a.zipfiles, a.datas, strip=False, upx=True, name='NAME')

然后运行

pyinstaller -y config.spec

执行失败,因为pyinstaller没有安装pulp包。执行pyinstaller后,转到dist\your_app,你会看到your_app包含除pulp之外的所有包。

如果您在虚拟环境中工作,请转到 your_env\Lib\site-packages 并复制所有与 pulp 相关的文件并将其粘贴到 dist\your_app

如果您不在虚拟环境中工作,请转至 AppData\Local\Programs\Python\Python36\Lib\site-packages 并执行上述相同的操作。

  1. 这个问题是因为一旦你运行pyinstaller,那么它就不会添加 与 pulp 的依赖关系相比,pulp 所需的依赖关系 所有其他包。

  2. 需要明确指定纸浆文件夹的路径。这 文件夹可以在python环境所在的目录下找到 已安装。

  3. pulp文件夹的路径可以在python路径中找到

Lib/site-packages/pulp

我们需要将纸浆的路径作为附加参数传递如下:


pyinstaller --noconfirm --onefile --console --add-data "C:/Users/.../Lib/site-packages/pulp;pulp/"  "C:/Users/.../python_to_exe.py"

第一个路径用于 pulp 目录,第二个路径用于具有 pulp 依赖性的 python 代码