简体   繁体   English

Pyomo 与 Python 3.11 的兼容性

[英]Pyomo Compatibility with Python 3.11

Is there a known compatibility issue with pyomo and python 3.11 ? pyomopython 3.11是否存在已知的兼容性问题?

Just trying out the latest/greatest python release and importing pyomo is failing with pyomo v 6.4.2:只是尝试最新/最好的 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 Version check: 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 isn't compatible with python 3.11. Pyomo 6.4.2 与 python 3.11 不兼容。 You need to update to 6.4.3, which was released on 28th November 2022.您需要更新到 2022 年 11 月 28 日发布的 6.4.3。

Previous answer - before 6.4.3 update以前的答案 - 6.4.3 更新之前

Verion 6.4.2 does some stuff with pickle and the __getstate__ / __setstate__ functions. Verion 6.4.2 使用 pickle 和__getstate__ / __setstate__函数做了一些事情。 However, Python 3.11 introduced a default implementation for these functions that does not match Pyomo's expectations.然而,Python 3.11 引入了这些功能的默认实现,这与 Pyomo 的期望不符。 So the whole thing falls over.所以整个事情都失败了。

See this note on the new default implementation for __getstate__ .请参阅有关__getstate__的新默认实现的__getstate__

Classes can further influence how their instances are pickled by overriding the method getstate ().类可以通过覆盖方法getstate () 进一步影响它们的实例如何被 pickle。 It is called and the returned object is pickled as the contents for the instance, instead of a default state. There are several cases:它被调用,返回的 object 被 pickle 为实例的内容,而不是默认的 state。有几种情况:

  • For a class that has no instance __dict__ and no __slots__ , the default state is None .对于没有实例__dict____slots__的 class,默认的 state 是None

  • For a class that has an instance __dict__ and no __slots__ , the default state is self.__dict__ .对于具有实例__dict__且没有__slots__的 class ,默认 state 是self.__dict__

  • For a class that has an instance __dict__ and __slots__ , the default state is a tuple consisting of two dictionaries: self.__dict__ , and a dictionary mapping slot names to slot values.对于具有实例__dict____slots__的 class ,默认 state 是一个由两个字典组成的元组: self.__dict__和一个将插槽名称映射到插槽值的字典。 Only slots that have a value are included in the latter.后者仅包含具有值的插槽。

  • For a class that has __slots__ and no instance __dict__ , the default state is a tuple whose first item is None and whose second item is a dictionary mapping slot names to slot values described in the previous bullet.对于具有__slots__且没有实例__dict__的 class,默认 state 是一个元组,其第一项为 None,其第二项是将插槽名称映射到上一条项目符号中所述的插槽值的字典。

Changed in version 3.11: Added the default implementation of the __getstate__() method in the object class.在 3.11 版更改:在 object class 中添加了__getstate__()方法的默认实现。

In the v6.4.2 code base you can see it does a check to see if __getstate__ is implemented or not.在 v6.4.2 代码库中,您可以看到它会检查__getstate__是否已实现。 https://github.com/Pyomo/pyomo/blob/6.4.2/pyomo/core/base/component.py#L467 https://github.com/Pyomo/pyomo/blob/6.4.2/pyomo/core/base/component.py#L467

In the main branch, this code has recently been changed.在主分支中, 此代码最近已更改。 And there is a recent commit saying that the code is now 3.11 compatible.最近有一个提交说代码现在与 3.11 兼容。 So just hang tight, and wait for the new version to be release.因此,请稍等片刻,等待新版本的发布。

Or if you really cannot wait and just want to experiment, then install the development version of 6.4.3 with git:或者,如果你真的等不及了,只想试验一下,那就用 git 安装 6.4.3 的开发版:

eg.例如。

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

NB.注意。 Obviously, you'll need git installed to be able to do this.显然,您需要安装 git 才能执行此操作。 And do not use this to install pyomo for production code.并且不要使用它来安装生产代码的 pyomo。 This is code that is under development and has not yet been released.这是正在开发中的代码,尚未发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM