Pyomo / pyomo

An object-oriented algebraic modeling language in Python for structured optimization problems.
https://www.pyomo.org
Other
1.97k stars 505 forks source link

pyomo.common.deprecation failure with 6.4.3 if installed from PyPI #2642

Closed lbianchi-lbl closed 1 year ago

lbianchi-lbl commented 1 year ago

Summary

This issue emerged as part of the IDAES release earlier today (a test in IDAES uses an import path that has been deprecated in pyomo 6.4.3, which is used below to reproduce the error), but it doesn't seem to be IDAES-specific.

It appears that the same version of Pyomo 6.4.3 results in different behaviors depending on whether it was installed from PyPI or from a source-like URL.

If pyomo 6.4.3 is installed from source, e.g. using pip install "pyomo @ https://github.com/Pyomo/pyomo/archive/6.4.3.zip", then the deprecation infrastructure seems to work as expected:

$ python -c "from pyomo.core.expr.logical_expr import EqualityExpression"
WARNING: DEPRECATED: the 'EqualityExpression' class has been moved to
    'pyomo.core.expr.relational_expr.EqualityExpression'.  Please update your
    import.  (deprecated in 6.4.3)

However, if the same version of Pyomo is installed from PyPI with pip install pyomo==6.4.3, the exact same import fails with ImportError:

$ python -c "from pyomo.core.expr.logical_expr import EqualityExpression"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: cannot import name 'EqualityExpression' from 'pyomo.core.expr.logical_expr' (/opt/conda/envs/test-pyomo-6.4.3/lib/python3.9/site-packages/pyomo/core/expr/logical_expr.cpython-39-x86_64-linux-gnu.so)

Steps to reproduce the issue

conda create --name test-pyomo-6.4.3-pypi --yes python=3.9 && conda activate test-pyomo-6.4.3-pypi
pip install pyomo==6.4.3
python -c "from pyomo.core.expr.logical_expr import EqualityExpression"  # ImportError
conda deactivate
conda create --name test-pyomo-6.4.3-gh --yes python=3.9 && conda activate test-pyomo-6.4.3-gh
pip install "pyomo @ https://github.com/Pyomo/pyomo/archive/6.4.3.zip"
python -c "from pyomo.core.expr.logical_expr import EqualityExpression"  # deprecation warning

Error Message

ImportError: cannot import name 'EqualityExpression' from 'pyomo.core.expr.logical_expr' (/opt/conda/envs/test-pyomo-6.4.3/lib/python3.9/site-packages/pyomo/core/expr/logical_expr.cpython-39-x86_64-linux-gnu.so)

Information on your system

Pyomo version: 6.4.3 Python version: 3.9 Operating system: Ubuntu 20.04.5 How Pyomo was installed (PyPI, conda, source): See above Solver (if applicable): N/A

jsiirola commented 1 year ago

OK - This is because the pypi wheels have some cythonized modules and the relocated_module_attribute function cannot correctly identify the calling module. #2644 will resolve this specific issue: both by updating relocated_module_attribute so that it can work with cythonized modules and by adding a check that should prevent this from happening in the future.