linopy
raw JSON → 0.6.7 verified Fri May 01 auth: no python
Linear optimization with N-D labeled arrays in Python. Integrates with xarray and supports multiple solvers (HiGHS, Gurobi, CPLEX, etc.). Current version 0.6.7, released occasionally with bugfixes and performance improvements.
pip install linopy Common errors
error NoSolverAvailable: No solver found. Install a solver like highspy, gurobipy, or cplex, or set the `solver` argument. ↓
cause No supported solver is installed or accessible in the environment.
fix
Install highspy: pip install highspy. Or install gurobipy if you have a license.
error ValueError: cannot add variable without a name ↓
cause You forgot to provide the name parameter in add_variables() or add_constraints().
fix
Always specify name='myvar' when adding variables or constraints.
error TypeError: unhashable type: 'DataArray' ↓
cause Using a DataArray as a dictionary key or index where hashable type is expected.
fix
Convert to tuple or string before use, or use .values.
Warnings
breaking In linopy v0.6.0, the auto_mask parameter was added to the Model class, changing its behavior for masked variables/constraints. Previously mask handling could be implicit; now the default is auto_mask=True which may mask out variables/constraints with zero coefficients. Set auto_mask=False to restore previous behavior. ↓
fix Set auto_mask=False when creating Model if you rely on unmasked behavior: Model(auto_mask=False)
deprecated The use of xarray.DataArray for coefficients in add_variables and add_constraints is encouraged over numpy arrays. Passing raw numpy arrays may lead to unexpected broadcasting or lack of named dimensions. ↓
fix Use xr.DataArray with proper dims/coords instead of np.ndarray.
gotcha Scalar getitem on LinearExpression or Variable (e.g., expr[0]) raised FutureWarning in older versions and may be removed in future. Use .values or .isel() instead. ↓
fix Replace var[0] with var.values[0] or var.isel(i=0) where i is the dimension name.
gotcha Linopy does not automatically install a solver. If no solver is found, solve() will raise an error. Common mistake: pip install linopy without solver, then call solve(). ↓
fix Install a solver like HiGHS (pip install highspy) or Gurobi, and ensure it is on PATH or properly configured.
Install
pip install linopy[gurobi] conda install -c conda-forge linopy Imports
- Model wrong
from linopy.model import Modelcorrectfrom linopy import Model - Variable wrong
from linopy.variables import Variablecorrectfrom linopy import Variable - LinearExpression wrong
from linopy.expressions import LinearExpressioncorrectfrom linopy import LinearExpression
Quickstart
from linopy import Model
import xarray as xr
m = Model()
# Create a variable with coords
x = m.add_variables(name='x', lower=0, coords=[('i', [0,1,2])])
# Add constraint
m.add_constraints(x.sum('i') <= 10, name='total')
# Add objective
m.add_objective(x.sum('i'))
# Solve
m.solve()
# Retrieve solution
print(x.solution)