Registry / data / cobra
library0.31.1pypypi✓ verified 88d ago

COBRApy (0.31.1) is a Python package for constraint-based modeling of metabolic networks. It enables building, simulating, and analyzing genome-scale metabolic models using flux balance analysis (FBA) and related methods. Releases occur several times a year, with a focus on stability and new features.

pip install cobra
INSTALL
IMPORT
SIG · COBRA
C
cobra
datapythonv0.31.1
Install
17.7s avg
Import
—
Disk
327MB
Pass rate
5/ 10
Env Coverage5 / 10
glibc
3.9–3.13
musl
3.9–3.13
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
musl
py 3.10–3.910 runs
build_error
glibc
py 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)
Debug
Known issues
breakingIn cobra 0.31, the default solver interface changed to optlang. If you rely on the older glpk interface directly, your code may break.
fix
Install cobra[optlang] and use optlang.glpk_interface (or other solver interfaces).
affects: >=0.31
gotchaSetting model.objective = reaction_object does not automatically change the objective coefficient; you must set coefficient explicitly via model.objective = {reaction: 1} or reaction.objective_coefficient = 1.
fix
Use model.objective = {reaction: 1} or reaction.objective_coefficient = 1 for a single reaction.
affects: all
deprecatedThe method 'cobra.io.sbml.write_sbml' is deprecated; use 'cobra.io.write_sbml_model' instead.
fix
Replace cobra.io.sbml.write_sbml with cobra.io.write_sbml_model.
affects: >=0.30
gotchaFlux balance analysis results are stored in cobra.Solution which is not subscriptable; use solution.fluxes (pandas Series) to access individual fluxes.
fix
Use solution.fluxes['REACTION_ID'] instead of solution['REACTION_ID'].
affects: all
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'cobra'
cobra is not installed or virtual environment not activated.
fix
Run '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).
fix
Use 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.
fix
Install 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.
Agent activity
17 hits · last 30 days
node
16
OpenAI (training)
1
Resources