Gurobi Python Interface (gurobipy)

13.0.1 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

This quickstart demonstrates a simple linear programming problem: maximizing `x + 2y` subject to `x + y <= 4` and `2x + y <= 5`. It shows how to create a model, add continuous variables, set an objective function, add constraints, and optimize the model. It also includes basic error handling for different optimization statuses.

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}")

view raw JSON →