使用auto-py-to-exe编译Gekko脚本为exe后remote=False模式报错的解决方法
Gekko编译为EXE后的运行问题及解决办法
问题描述
1. Remote=False模式编译后报错
在VSCode中以remote="False"、m.options.SOLVER=3模式运行Gekko应用完全正常,但使用auto-py-to-exe将其编译为EXE文件后,运行时求解器抛出以下错误:
Error: The system cannot find the specified path.
Error: 'results.json' not found. Check above for more error details,
[Errno 2] No such file or directory: 'C:\Users\****\AppData\Local\Temp\tmpfzmt2vvbgk_model0\options.json'
执行m.open_folder()可以打开Gekko的临时文件夹路径,但关键的求解器相关文件无法创建。
2. Remote=True指定localhost的问题
尝试设置remote="True"并指定服务器为localhost时,浏览器能正常访问apmonitor服务器,但代码中使用m = GEKKO(remote=True, server="http://127.0.0.1")时会提示:
Server not found or unreachable.
而使用remote="True"搭配默认服务器时,无论是源码还是编译后的EXE文件都能正常运行。
解决方案
重点解决Remote=False模式问题
编译Gekko应用时,核心问题是打包工具未包含Gekko依赖的必要数据文件,针对不同打包工具的配置方式如下:
- auto-py-to-exe:需添加
--hidden-import和--collect-data参数,在打包配置中引入from PyInstaller.utils.hooks import collect_data_files并指定目标为gekko;同时确保已安装pyinstaller和auto-py-to-exe两个依赖包。 - PyInstaller/py2exe:同样需要配置数据文件收集逻辑,通过PyInstaller的钩子机制收集Gekko的全部数据文件,确保编译后的EXE能找到求解器运行所需的文件。
内容的提问来源于stack exchange,提问作者Mohammad
相关产品推荐
相关产品推荐

