SolverStudio运行外部Python时遇ImportError:无法从time导入clock
问题:SolverStudio运行外部Python时遇ImportError错误
- 运行场景:在SolverStudio中尝试运行PuLP示例表的「Transportation-PuLP External」标签页模型
- 模型开头代码:
# When running External Python, we use the following to connect to SolverStudio from SolverStudio import * # Import PuLP modeller functions from pulp import * - 报错信息:
from time import clock ImportError: cannot import name 'clock' from 'time' (unknown location) ## CPython did not complete (Error Code 1); no solution is available. ## Done - 已尝试操作:执行
pip install passlib==1.7.2和pip install --upgrade passlib==1.7.2安装/升级passlib至1.7.2版本,问题未解决 - 补充:相关表格及代码可从SolverStudio压缩包获取
解决方法
- 检查Python版本:确认所用Python版本是否为3.8及以上,
time.clock()在Python3.8中已被废弃并移除,这是报错的核心原因。 - 升级PuLP到兼容版本:运行命令
pip install --upgrade pulp,新版本PuLP已修复该兼容性问题,适配Python3.8+环境。 - 临时修复依赖库代码:若升级PuLP无效,找到触发报错的依赖文件,将
from time import clock替换为:
或直接替换为try: from time import clock except ImportError: from time import perf_counter as clockfrom time import perf_counter as clock(perf_counter统计含休眠的总时间,若需统计CPU时间可改用process_time)。 - 核对SolverStudio的Python环境:确保SolverStudio调用的是已完成PuLP升级的Python环境,避免环境不一致导致修复无效。
内容的提问来源于stack exchange,提问作者Incognito
相关产品推荐
相关产品推荐

