I’m working with Pyomo in a Python environment, and I’ve encountered an AttributeError when trying to import Pyomo modules. The error message states that the module ‘pyomo.common’ has no attribute ‘errors’. Here’s the code snippet that causes the error:
from pyomo.environ import *
model = ConcreteModel()
# Variables
model.x = Var(range(len(wh_capacities_df)), within=NonNegativeReals)
# Objective
model.obj = Objective(expr=sum((model.x[i] - demand_per_day[i])**2 for i in range(len(demand_per_day))), sense=minimize)
# Constraints
model.constraints = ConstraintList()
for i in range(len(wh_capacities_df)):
model.constraints.add(model.x[i] <= wh_capacities_df['Daily Capacity '].iloc[i])
# Solve
SolverFactory('glpk').solve(model)
# Display results
for i in range(len(wh_capacities_df)):
print(f"Optimal capacity for plant {wh_capacities_df['Plant ID'].iloc[i]}: {model.x[i].value}")
full error message
{
"name": "AttributeError",
"message": "module 'pyomo.common' has no attribute 'errors'",
"stack": "---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[32], line 1
----> 1 from pyomo.environ import *
3 model = ConcreteModel()
5 # Variables
File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pyomo\environ\__init__.py:86
82 pkg = _sys.modules[pname]
83 pkg.load()
---> 86 _import_packages()
88 #
89 # Expose the symbols from pyomo.core
90 #
91 from pyomo.dataportal import DataPortal
File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pyomo\environ\__init__.py:66, in _import_packages()
64 pname = _package + '.plugins'
65 try:
---> 66 _do_import(pname)
67 except ImportError:
68 exctype, err, tb = _sys.exc_info() # BUG?
File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pyomo\environ\__init__.py:18, in _do_import(pkg_name)
17 def _do_import(pkg_name):
---> 18 importlib.import_module(pkg_name)
File C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.1520.0_x64__qbz5n2kfra8p0\Lib\importlib\__init__.py:90, in import_module(name, package)
88 break
89 level += 1
---> 90 return _bootstrap._gcd_import(name[level:], package, level)
File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pyomo\core\__init__.py:83
69 from pyomo.core.expr.symbol_map import SymbolMap
70 from pyomo.core.expr import (
71 numvalue,
72 numeric_expr,
(...)
81 calculus,
82 )
---> 83 from pyomo.core import expr, util, kernel
85 from pyomo.core.expr.numvalue import (
86 nonpyomo_leaf_types,
87 PyomoObject,
(...)
96 ZeroConstant,
97 )
98 from pyomo.core.expr.boolean_value import (
99 as_boolean,
100 BooleanConstant,
101 BooleanValue,
102 native_logical_values,
103 )
File pyomo\\core\\util.pyx:19, in init pyomo.core.util()
File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pyomo\core\base\__init__.py:38
18 from pyomo.core.expr.numvalue import (
19 nonpyomo_leaf_types,
20 native_types,
(...)
29 ZeroConstant,
30 )
31 from pyomo.core.expr.boolean_value import (
32 as_boolean,
33 BooleanConstant,
34 BooleanValue,
35 native_logical_values,
36 )
---> 38 from pyomo.core.base.component import name, Component, ModelComponentFactory
39 from pyomo.core.base.componentuid import ComponentUID
40 from pyomo.core.base.config import PyomoOptions
File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pyomo\core\base\component.py:79
74 @deprecated(msg="The cname() function has been renamed to name()", version='5.6.9')
75 def cname(*args, **kwds):
76 return name(*args, **kwds)
---> 79 class CloneError(pyomo.common.errors.PyomoException):
80 pass
83 class ComponentBase(PyomoObject):
AttributeError: module 'pyomo.common' has no attribute 'errors'"
}
I’ve tried updating Pyomo and checking for any conflicts with other packages, but the issue persists. I’m using Python 3.12 and Pyomo version 6.7.3 and downgrade to Pyomo-5.7.3 also didnt worked. Has anyone else encountered this issue, or does anyone know how to resolve it?
nuwan desilva is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.