Pyomo 6.4.2与Python 3.11是否存在已知兼容性问题?
Pyomo 6.4.2 与 Python 3.11 导入报错的兼容性问题
问题描述
尝试使用Python 3.11导入Pyomo 6.4.2时失败,报错信息如下:
Python 3.11.0 (v3.11.0:deaf509e8f, Oct 24 2022, 14:43:23) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from pyomo.environ import * Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyomo/environ/__init__.py", line 79, in <module> _import_packages() File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyomo/environ/__init__.py", line 61, in _import_packages _do_import(pname) File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyomo/environ/__init__.py", line 16, in _do_import importlib.import_module(pkg_name) File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyomo/core/__init__.py", line 43, in <module> from pyomo.core import expr, util, kernel File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyomo/core/util.py", line 21, in <module> from pyomo.core.base.var import Var File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyomo/core/base/__init__.py", line 34, in <module> from pyomo.core.base.label import (CuidLabeler, File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyomo/core/base/label.py", line 19, in <module> from pyomo.core.base.componentuid import ComponentUID File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyomo/core/base/componentuid.py", line 25, in <module> from pyomo.core.base.reference import Reference File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyomo/core/base/reference.py", line 17, in <module> from pyomo.core.base.set import SetOf, OrderedSetOf, _SetDataBase File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyomo/core/base/set.py", line 4208, in <module> DeclareGlobalSet(_AnySet( File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyomo/core/base/set.py", line 4199, in DeclareGlobalSet _set.__class__.__setstate__(_set, obj.__getstate__()) ^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyomo/core/base/component.py", line 787, in __getstate__ state = _base.__getstate__() ^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyomo/core/base/indexed_component.py", line 316, in __getstate__ state = super(IndexedComponent, self).__getstate__() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyomo/core/base/component.py", line 471, in __getstate__ state[key] = val ~~~~~^^^^^ TypeError: 'tuple' object does not support item assignment >>>
Pyomo版本信息
~ % pip3 show pyomo Name: Pyomo Version: 6.4.2 Summary: Pyomo: Python Optimization Modeling Objects Home-page: http://pyomo.org Author: Author-email: License: BSD Location: /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages Requires: ply Required-by: ~ %
解答
这是Pyomo 6.4.2版本与Python 3.11的已知兼容性问题。Python 3.11对__getstate__方法的返回值处理有变更,而Pyomo 6.4.2的代码中,component.py里的__getstate__方法错误地将返回的元组当作字典进行赋值操作,导致触发TypeError。
解决方法:
- 升级Pyomo到6.5.0及以上版本:后续版本已经修复了该兼容性问题,执行
pip install --upgrade pyomo即可更新。 - 降级Python版本:如果暂时无法升级Pyomo,可以切换到Python 3.10或更低版本来规避该问题。
内容的提问来源于stack exchange,提问作者AirSquid
相关产品推荐
相关产品推荐

