Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error when xpress is imported before xpress_direct #3410

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
)
Loading