python-libsbml

raw JSON →
5.21.1 verified Mon Apr 27 auth: no python

Python bindings for LibSBML, a library for reading, writing, and manipulating SBML (Systems Biology Markup Language) documents. This package provides the libsbml module, which wraps the C++ library SBML API with Python objects. Version 5.21.1 is current; releases follow the upstream LibSBML releases.

pip install python-libsbml
error ImportError: No module named libsbml
cause Package not installed or wrong Python environment.
fix
Install with: pip install python-libsbml. Ensure activating the correct virtual environment.
error AttributeError: module 'libsbml' has no attribute 'SBMLDocument'
cause This can happen if there is a name collision: the variable 'libsbml' is redefined or overshadowed.
fix
Do not assign to the name 'libsbml' after importing. Use a different variable for your document: doc = libsbml.SBMLDocument()
breaking The package name on PyPI is 'python-libsbml', but the import is 'libsbml'. Do not use 'import sbml' or 'import libSBML'.
fix Use 'import libsbml'.
gotcha SBMLDocument constructor takes (level, version) as arguments, but the default is (2, 4) in older LibSBML versions. Always specify if you need a specific SBML level.
fix Explicitly pass level and version when creating a new document: libsbml.SBMLDocument(3, 2)
gotcha Error handling: writeSBML returns an integer status code, not a boolean. Checking 'if result:' may be misleading because LIBSBML_OPERATION_SUCCESS is 0 (falsy).
fix Compare to libsbml.LIBSBML_OPERATION_SUCCESS explicitly: if result != libsbml.LIBSBML_OPERATION_SUCCESS.

Create an SBML Document, write to file, and read it back. Checks for operation success and errors.

import libsbml
import sys

document = libsbml.SBMLDocument(3, 2)  # SBML Level 3 Version 2
model = document.createModel()
model.setId("my_model")

# Write to file
writer = libsbml.SBMLWriter()
result = writer.writeSBML(document, "output.xml")
if result != libsbml.LIBSBML_OPERATION_SUCCESS:
    print("Error writing SBML")
    sys.exit(1)

# Read back
reader = libsbml.SBMLReader()
document2 = reader.readSBML("output.xml")
if document2.getNumErrors() > 0:
    print("Errors encountered:")
    document2.printErrors()