Registry / devops / swiglpk

swiglpk

JSON →
library5.0.13pypypi✓ verified 83d ago

swiglpk provides simple SWIG-generated Python bindings for the GNU Linear Programming Kit (GLPK), a library for solving linear programming (LP) and mixed integer programming (MIP) problems. Current version is 5.0.13 with an irregular release cadence.

pip install swiglpk
INSTALL
IMPORT
SIG · SWIGLPK
S
swiglpk
devopspythonv5.0.13
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

glpk
import swiglpk
import glpk
The module is named swiglpk, not glpk.

Quickstart solving a simple LP with two variables and one constraint using swiglpk.

import swiglpk # Create problem object lp = swiglpk.glp_create_prob() swiglpk.glp_set_prob_name(lp, 'sample') # Add columns (variables) swiglpk.glp_add_cols(lp, 2) swiglpk.glp_set_col_name(lp, 1, 'x1') swiglpk.glp_set_col_bnds(lp, 1, swiglpk.GLP_LO, 0.0, 0.0) swiglpk.glp_set_col_name(lp, 2, 'x2') swiglpk.glp_set_col_bnds(lp, 2, swiglpk.GLP_LO, 0.0, 0.0) # Add rows (constraints) swiglpk.glp_add_rows(lp, 1) swiglpk.glp_set_row_name(lp, 1, 'c1') swiglpk.glp_set_row_bnds(lp, 1, swiglpk.GLP_UP, 0.0, 100.0) # Set objective coefficients swiglpk.glp_set_obj_coef(lp, 1, 10.0) swiglpk.glp_set_obj_coef(lp, 2, 6.0) # Set coefficients for constraint rows # (indexing: 1-based for rows, columns, and matrix) rows = [1, 1] cols = [1, 2] values = [1.0, 1.0] swiglpk.glp_set_mat_row(lp, 1, 2, rows, cols, values) # Actually: indices, values? Check API # Solve swiglpk.glp_simplex(lp, None) # Get solution print('Objective value:', swiglpk.glp_get_obj_val(lp)) print('x1 =', swiglpk.glp_get_col_prim(lp, 1)) print('x2 =', swiglpk.glp_get_col_prim(lp, 2)) # Clean up swiglpk.glp_delete_prob(lp)
Debug
Known issues
gotchaAll indices in GLPK (rows, columns, matrix entries) are 1-based, not 0-based. Common mistake to use 0-based indexing.
fix
Start row/column indices from 1.
affects: all
gotchaThe function glp_set_mat_row() expects arguments: (lp, row, len, ind, val) where ind and val are lists of indices and values. The indices must be 1-based column indices. Refer to GLPK documentation for correct usage.
fix
Provide ind as list of column indices starting at 1, and val as list of corresponding coefficients.
affects: all
gotchaMemory management: created problems (glp_create_prob) must be explicitly deleted with glp_delete_prob to avoid leaks.
fix
Call glp_delete_prob(lp) after use.
affects: all
gotchaThe library does not have Python-friendly wrappers; you must use the low-level C-style API. Objects like glp_prob are opaque pointers.
fix
Accept the procedural API; consider using higher-level wrappers like pulp or cvxopt for simpler tasks.
affects: all
Errors
Common errors & fixes
ImportError: No module named swiglpk
swiglpk not installed or installed in wrong environment.
fix
Run 'pip install swiglpk' in the correct Python environment.
OSError: libglpk.so: cannot open shared object file: No such file or directory
The GLPK shared library is missing from the system.
fix
Install GLPK via system package manager (e.g., apt-get install glpk-utils libglpk-dev on Debian/Ubuntu) or via conda: conda install -c conda-forge glpk.
glp_set_mat_row: len = 2; ind[1] = 0; column index out of range
Indices in ind list are 0-based, but GLPK expects 1-based indices.
fix
Change ind list to start from 1. For example, for columns 1 and 2, use ind = [1, 2].
Upgrade
Version history
5.0.13latest on PyPI · released Feb 5, 2026
Audit
Dependencies
glpkrequiredRequires GLPK shared library (libglpk) installed on system or via conda
Agent activity
7 hits · last 30 days
node
6
Resources
swiglpk — pip install swiglpk · libregistry