Gurobi Python Interface (gurobipy)
gurobipy is the official Python interface to the Gurobi Optimizer, a powerful mathematical optimization software library for solving mixed-integer linear, quadratic, and quadratically constrained programming problems. It provides convenient object-oriented modeling constructs and an API to all Gurobi features. The current version is 13.0.1, released on January 21, 2026, and it follows a regular release cadence with major versions typically released annually.
Warnings
- gotcha Gurobi Optimizer requires a license for full functionality. While `pip install gurobipy` includes a limited trial license, solving larger or commercial problems necessitates obtaining and configuring a separate Gurobi license key. Academic licenses are available for students and faculty.
- breaking Preview features introduced in Gurobi 13.0, such as the new nonlinear barrier method, are explicitly noted as potentially undergoing significant changes in subsequent releases, 'including breaking changes in API, behavior or packaging'. This means code relying on these specific preview features may require adjustments in future minor or major Gurobi updates.
- gotcha For optimal performance and to utilize the full Gurobi Optimizer capabilities, it is often recommended to download and install the full Gurobi Optimizer package from the Gurobi website, in addition to `pip installing` `gurobipy`. The `pip` package alone might use a more basic or limited installation.
- breaking Older versions of Python (e.g., Python 2.7, and specific older Python 3.x versions) are only compatible with corresponding older Gurobi releases. Attempting to use a recent `gurobipy` with an incompatible Python version will lead to errors. While PyPI lists `>=3.10` for `gurobipy` 13.0.1, Gurobi's general compatibility chart suggests broader (but version-dependent) support.
Install
-
pip install gurobipy -
conda install gurobi::gurobipy
Imports
- gurobipy
import gurobipy as gp
- GRB
from gurobipy import GRB
Quickstart
import gurobipy as gp
from gurobipy import GRB
import os
# NOTE: Gurobi requires a valid license. A trial license is included with pip install,
# but for full functionality, a separate Gurobi Optimizer installation and license key
# (e.g., academic or commercial) is typically required.
# For demonstration, we'll assume a license is configured or a small problem is solved.
# For academic license info: https://www.gurobi.com/downloads/end-user-license-agreement-academic/
# Create a new model
m = gp.Model("quickstart_model")
# Create variables
x = m.addVar(vtype=GRB.CONTINUOUS, name="x", lb=0.0)
y = m.addVar(vtype=GRB.CONTINUOUS, name="y", lb=0.0)
# Set objective: Maximize x + 2y
m.setObjective(x + 2 * y, GRB.MAXIMIZE)
# Add constraint: x + y <= 4
m.addConstr(x + y <= 4, "c0")
# Add constraint: 2x + y <= 5
m.addConstr(2 * x + y <= 5, "c1")
# Optimize model
m.optimize()
if m.status == GRB.OPTIMAL:
print(f"Optimal objective: {m.objVal}")
print(f"x: {x.X}, y: {y.X}")
elif m.status == GRB.UNBOUNDED:
print("Model is unbounded")
elif m.status == GRB.INF_OR_UNBOUNDED:
print("Model is either infeasible or unbounded")
elif m.status == GRB.INFEASIBLE:
print("Model is infeasible")
else:
print(f"Optimization ended with status {m.status}")