diff --git a/pyomo/solvers/plugins/solvers/xpress_direct.py b/pyomo/solvers/plugins/solvers/xpress_direct.py index 0e7f4dd7ce3..43316603a41 100644 --- a/pyomo/solvers/plugins/solvers/xpress_direct.py +++ b/pyomo/solvers/plugins/solvers/xpress_direct.py @@ -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 @@ -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, +)