与Python 3.11的兼容性

cgvd09ve  于 2022-12-01  发布在  Python
关注(0)|答案(1)|浏览(1092)

pyomopython 3.11是否存在已知的兼容性问题?
仅仅尝试最新/最好的python版本并导入pyomo在pyomo v 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: 
~ %
3phpmpom

3phpmpom1#

Pyomo 6.4.2与python 3.11不兼容。您需要更新到2022年11月28日发布的6.4.3。

上一个答案-6.4.3更新之前

Verion 6.4.2对pickle和__getstate__/__setstate__函数做了一些事情。然而,Python 3.11为这些函数引入了一个默认的实现,这并不符合Pyomo的期望。所以整个事情都失败了。
请参阅有关__getstate__的新默认实现的说明。
类可以通过重写方法getstate()进一步影响示例的pickle方式。调用该方法,返回的对象将被pickle为示例的内容,而不是默认状态。有几种情况:

  • 对于没有示例__dict____slots__的类,默认状态为None
  • 对于具有示例__dict__但没有__slots__的类,默认状态为self.__dict__
  • 对于具有示例__dict____slots__的类,默认状态是由两个字典组成的元组:self.__dict__,以及一个将插槽名称Map到插槽值的字典。后者只包含具有值的插槽。
  • 对于具有__slots__但没有示例__dict__的类,默认状态是一个元组,其第一项是None,第二项是将插槽名称Map到上一项中描述的插槽值的字典。

版本3.11中的变更:在对象类中添加了__getstate__()方法的默认实现。
在v6.4.2代码库中,您可以看到它会检查__getstate__是否已实现。https://github.com/Pyomo/pyomo/blob/6.4.2/pyomo/core/base/component.py#L467
在主分支中,这段代码最近被修改了。而且最近有一个commit说这段代码现在是3.11兼容的。所以只要坚持住,等待新版本的发布。
或者,如果你实在等不及了,只想尝试一下,那就用git安装6.4.3的开发版本:
例如

pip install git+https://github.com/Pyomo/pyomo.git@main

注意:显然,你需要安装git才能做到这一点。不要用它来安装生产代码的pyomo。这是正在开发中的代码,还没有发布。

相关问题