Install & Compatibility
Where this runs
tested against v0.31.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.910 runs
build_error
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 17.7s · import 0.000s · 304MB
327MB installed
● package 327MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Model
✓ from cobra import Model
✗ from cobra.core import Model
cobra.core provides internals; public API is from cobra.
Reaction
✓ from cobra import Reaction
✗ from cobra.core import Reaction
Public API from cobra top-level.
Metabolite
✓ from cobra import Metabolite
✗ from cobra.core import Metabolite
Top-level import is correct.
flux_analysis
✓ from cobra import flux_analysis
✗ from cobra.flux_analysis import pfba
Use flux_analysis.pfba, flux_analysis.single_gene_deletion, etc.
Create a simple model, add a reaction, set objective, and solve using FBA.
from cobra import Model, Reaction, Metabolite
model = Model('example')
# Add reaction: A -> B
reaction = Reaction('R1')
reaction.name = 'A to B'
reaction.subsystem = 'Exchange'
reaction.lower_bound = 0. # irreversible
reaction.upper_bound = 1000.
metabolite_a = Metabolite('A_c', compartment='c')
metabolite_b = Metabolite('B_c', compartment='c')
reaction.add_metabolites({metabolite_a: -1, metabolite_b: 1})
model.add_reactions([reaction])
model.objective = 'R1' # maximize flux through R1
solution = model.optimize()
print(solution.objective_value)
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'cobra'
cobra is not installed or virtual environment not activated.
fixRun 'pip install cobra' in your terminal.
ValueError: objective must be a scalar (reaction) or dict of reactions to coefficients
Setting model.objective = ['R1', 'R2'] incorrectly (list instead of dict or single reaction).
fixUse model.objective = 'R1' for a single reaction or model.objective = {'R1': 1, 'R2': 0.5} for multiple. No solver interface found
optlang not installed and no solver (e.g., GLPK, CPLEX) available.
fixInstall optlang: 'pip install cobra[optlang]' or install a solver like GLPK.
Upgrade
Version history
0.31.1latest on PyPI · released Mar 26, 2026
Audit
Dependencies
optlangoptionalDefault LP solver interface (GLPK, CPLEX, Gurobi). Optional but recommended.
pandasrequiredRequired for DataFrame-based results and model I/O.
libsbmloptionalRequired for SBML file support.