我正试图用Excel和Python开发一个混合整数线性规划(MILP)优化工具。Pyomo与GLPK解算器被用于MILP优化。Python程序从excel文件中读取输入,并将输出写入excel文件。我使用Pyinstaller来创建exe文件。当exe文件按预期打开时,程序可以工作。
问题-我想从excel文件中使用VBA宏触发exe文件。当使用Shell()命令从VBA触发exe文件时,程序会出现以下错误-- "ValueError:未能为解决程序glpk设置可执行文件。名为=glpk-4.65\w64\glpsol.exe的文件要么不存在,要么无法执行。要跳过此验证,请用validate=False调用set_executable。“
当exe被直接使用时,程序运行良好,当exe文件通过VBA触发时,相同的程序会产生上述错误。请帮我解决这个问题。
编辑:
调用exe的VBA代码的一部分
With CreateObject("WScript.Shell")
.Run """" + NewFilePath + """", 1, True
End With
NewFilePath是exe文件的位置。
请注意,GLPK解决程序在exe被直接执行时工作。当从VBA宏调用时,除了GLPK之外,python程序的其他部分也可以工作。当从宏调用exe时,唯一不起作用的是GLPK。
错误信息-
WARNING: Failed to create solver with name '_glpk_shell': Failed to set
executable for solver glpk. File with name=glpk-4.65\w64\glpsol.exe either
does not exist or it is not executable. To skip this validation, call
set_executable with validate=False.
Traceback (most recent call last):
File "pyomo\opt\base\solvers.py", line 152, in __call__
File "pyomo\solvers\plugins\solvers\GLPK.py", line 119, in __init__
File "pyomo\opt\solver\shellcmd.py", line 55, in __init__
File "pyomo\opt\solver\shellcmd.py", line 100, in set_executable
ValueError: Failed to set executable for solver glpk. File with name=glpk-4.65\w64\glpsol.exe either does not exist or it is not executable. To skip this validation, call set_executable with validate=False.
Traceback (most recent call last):
File "optimization.py", line 459, in <module>
File "pyomo\opt\base\solvers.py", line 105, in solve
File "pyomo\opt\base\solvers.py", line 122, in _solver_error
RuntimeError: Attempting to use an unavailable solver.
The SolverFactory was unable to create the solver "_glpk_shell"
and returned an UnknownSolver object. This error is raised at the point
where the UnknownSolver object was used as if it were valid (by calling
method "solve").
The original solver was created with the following parameters:
executable: glpk-4.65\w64\glpsol.exe
type: _glpk_shell
_args: ()
options: {}
[15784] Failed to execute script 'optimization' due to unhandled exception!
发布于 2022-06-05 02:06:07
我解决了问题。VBA从cmd默认文件夹触发exe文件。Python/Pyomo在同一个文件夹中查找解决程序。在VBA中,通过转到exe和GLPK解决程序来自cmd的文件夹,并触发exe使程序工作。以下为更改
With CreateObject("WScript.Shell")
Cmd1 = "cd " & DistFolder
Cmd2 = """" + NewFilePath + """"
Connector = " & "
Commands = "cmd.exe /K " & Cmd1 & Connector & Cmd2
.Run Commands, 1, True
End With
@蒂姆·威廉姆斯的评论可能是对的。
https://stackoverflow.com/questions/72492465
复制相似问题