Skip to content

Commit dfa97dd

Browse files
committed
Prevent gurobipy from being imported with environ
1 parent a9d049b commit dfa97dd

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

pyomo/contrib/solver/solvers/gurobi_direct_minlp.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,19 @@
9898

9999
_function_map = {}
100100

101-
gurobipy, gurobipy_available = attempt_import('gurobipy', minimum_version='12.0.0')
102-
if gurobipy_available:
103-
from gurobipy import GRB, nlfunc
104101

102+
def _finalize_gurobipy(gurobipy, available):
103+
if not available:
104+
return
105105
_function_map.update(
106106
{
107-
'exp': (_GENERAL, nlfunc.exp),
108-
'log': (_GENERAL, nlfunc.log),
109-
'log10': (_GENERAL, nlfunc.log10),
110-
'sin': (_GENERAL, nlfunc.sin),
111-
'cos': (_GENERAL, nlfunc.cos),
112-
'tan': (_GENERAL, nlfunc.tan),
113-
'sqrt': (_GENERAL, nlfunc.sqrt),
107+
'exp': (_GENERAL, gurobipy.nlfunc.exp),
108+
'log': (_GENERAL, gurobipy.nlfunc.log),
109+
'log10': (_GENERAL, gurobipy.nlfunc.log10),
110+
'sin': (_GENERAL, gurobipy.nlfunc.sin),
111+
'cos': (_GENERAL, gurobipy.nlfunc.cos),
112+
'tan': (_GENERAL, gurobipy.nlfunc.tan),
113+
'sqrt': (_GENERAL, gurobipy.nlfunc.sqrt),
114114
# Not supporting any of these right now--we'd have to build them from the
115115
# above:
116116
# 'asin': None,
@@ -128,6 +128,15 @@
128128
)
129129

130130

131+
gurobipy, gurobipy_available = attempt_import(
132+
'gurobipy',
133+
deferred_submodules=['GRB'],
134+
callback=_finalize_gurobipy,
135+
minimum_version='12.0.0',
136+
)
137+
GRB = gurobipy.GRB
138+
139+
131140
"""
132141
In Gurobi 12:
133142

0 commit comments

Comments
 (0)