Skip to content

Commit

Permalink
Merge pull request Pyomo#3410 from jsiirola/xpress-import
Browse files Browse the repository at this point in the history
Fix error when xpress is imported before xpress_direct
  • Loading branch information
jsiirola authored Nov 8, 2024
2 parents c662b35 + 22a9dd1 commit 364e7f1
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions pyomo/solvers/plugins/solvers/xpress_direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,21 +197,6 @@ def __call__(self):
return xpress


_xpress_importer = _xpress_importer_class()
xpress, xpress_available = attempt_import(
'xpress',
error_message=_xpress_importer,
# Other forms of exceptions can be thrown by the xpress python
# import. For example, an xpress.InterfaceError exception is thrown
# if the Xpress license is not valid. Unfortunately, you can't
# import without a license, which means we can't test for that
# explicit exception!
catch_exceptions=(Exception,),
importer=_xpress_importer,
callback=_finalize_xpress_import,
)


@SolverFactory.register('xpress_direct', doc='Direct python interface to XPRESS')
class XpressDirect(DirectSolver):
_name = None
Expand Down Expand Up @@ -1222,3 +1207,21 @@ def load_slacks(self, cons_to_load=None):
"""
self._load_slacks(cons_to_load)


# Note: because _finalize_xpress_import references XpressDirect, we need
# to make sure to not attempt the xpress import until after the
# XpressDirect class is fully declared.
_xpress_importer = _xpress_importer_class()
xpress, xpress_available = attempt_import(
'xpress',
error_message=_xpress_importer,
# Other forms of exceptions can be thrown by the xpress python
# import. For example, an xpress.InterfaceError exception is thrown
# if the Xpress license is not valid. Unfortunately, you can't
# import without a license, which means we can't test for that
# explicit exception!
catch_exceptions=(Exception,),
importer=_xpress_importer,
callback=_finalize_xpress_import,
)

0 comments on commit 364e7f1

Please sign in to comment.