Python API Reference¶
The ontoenv Python package exposes the full Rust core through PyO3 bindings, with native rdflib graph interop. Pre-built wheels are published on PyPI — no Rust toolchain required.
There are two distinct Python integration surfaces:
OntoEnvfor ontology discovery, import resolution, and closure materialization.OntoEnvStorefor using ontoenv as anrdflibStorewith Rust-backed SPARQL execution.
Install¶
pip install ontoenv # Python 3.9+
Key methods¶
OntoEnv(search_directories, includes, offline, …)— Create or open an environment. Acceptssearch_directories(paths to crawl),offline(skip network),temporary(keep everything in memory), glob/regex filters, and a customgraph_store.env.update(all=False)— Re-run discovery with the configured directories. Passall=Trueto force re-fetching of all remote ontologies regardless of cache age.env.add(location, fetch_imports=True)— Register an ontology from a file path, URL, or an in-memoryrdflib.Graphthat contains anowl:Ontologydeclaration. Setfetch_imports=Falseto store only the root graph.env.get_closure(name, destination_graph=None, recursion_depth=-1)— Return a merged(Graph, int)pair — the ontology namednameplus all its transitive imports, and the count of imported graphs. Pass adestination_graphto merge into an existing graph in place.env.get_graph(name)— Return the storedrdflib.Graphfor a single ontology IRI — useful when you only need one graph rather than a full closure.env.import_dependencies(graph, fetch_missing=False)— Mutate anrdflib.Graphin place, inserting triples from all ontologies declared in itsowl:importsstatements. Setfetch_missing=Trueto download any imports not yet in the environment.
Example¶
from pathlib import Path
from ontoenv import OntoEnv
env = OntoEnv(
search_directories=["./ontologies"],
includes=["*.ttl"],
strict=False,
)
# Add a remote ontology and follow its imports
env.add("https://brickschema.org/schema/1.4.4/Brick.ttl")
# Retrieve just the Brick graph (no imports merged)
brick = env.get_graph("https://brickschema.org/schema/1.4/Brick")
# Retrieve Brick with all transitive imports merged
g, n_imports = env.get_closure("https://brickschema.org/schema/1.4/Brick")
print(f"Merged {n_imports} imports — {len(g)} triples total")
Note
Custom storage: Pass a graph_store= object to route all graph reads and writes
through your own backend. This is separate from the built-in rdflib store integration.
See Graph Store Interface for the protocol, or
RDFLib Store to query graphs directly through rdflib.