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)
Errors
Common errors & fixes
ImportError: No module named swiglpk
swiglpk not installed or installed in wrong environment.
fixRun '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.
fixInstall 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.
fixChange 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