Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

OntoEnv Python Bindings

Installation

pip install ontoenv

Usage

from ontoenv import OntoEnv, UnresolvedImportError
from rdflib import Graph

# Connect creates on first use and graph-free warm-opens on later runs.
env = OntoEnv.connect(".")

# add an ontology from a file path.
# env.add returns the name of the ontology, which is its URI
# e.g. "https://brickschema.org/schema/1.4-rc1/Brick"
brick_name = env.add("../brick/Brick.ttl")
print(f"Added ontology {brick_name}")

# When you add from a URL whose declared ontology name differs (for example a
# versioned IRI served at a versionless URL), ontoenv records that alias. You
# can later refer to the ontology by either the canonical name or the original
# URL when resolving imports or querying.

# get the graph of the ontology we just added
# env.get_graph returns a read-only store-backed rdflib.Graph
brick_graph = env.get_graph(brick_name)
print(f"Brick graph has {len(brick_graph)} triples")

# if you need a mutable in-memory graph, copy it explicitly
mutable_brick_graph = env.copy_graph(brick_name)

# get a read-only view of the full closure of the ontology, including all of its imports
# returns a tuple (ViewGraph, list[str])
brick_closure_graph, _ = env.get_closure(brick_name)
print(f"Brick closure has {len(brick_closure_graph)} triples")

# if you need a mutable materialized closure, copy it explicitly
mutable_brick_closure_graph, _ = env.copy_closure(brick_name)

# you can also add ontologies from a URL
rec_name = env.add("https://w3id.org/rec/rec.ttl")
rec_graph = env.get_graph(rec_name)
print(f"REC graph has {len(rec_graph)} triples")

# you can add an in-memory rdflib.Graph directly
in_memory = Graph()
in_memory.parse(data="""
@prefix owl: <http://www.w3.org/2002/07/owl#> .
<http://example.com/in-memory> a owl:Ontology .
""", format="turtle")
in_memory_name = env.add(in_memory)
print(f"Added in-memory ontology {in_memory_name}")

# if you have an rdflib.Graph with an owl:imports declaration,
# you can transitively import its dependencies into the graph
g = Graph()
g.parse(data="""
@prefix owl: <http://www.w3.org/2002/07/owl#> .
<http://example.com/application> a owl:Ontology ;
    owl:imports <https://brickschema.org/schema/1.4/Brick> .
""", format="turtle")
# Fetch imports not already cached and merge the available closure into g.
# In non-strict mode unavailable imports are skipped without leaving a
# catalog.pending recovery marker.
env.import_dependencies(g, fetch_missing=True)
print(f"Graph with imported dependencies has {len(g)} triples")

# Known unresolved targets have a dedicated exception type.
for missing in env.missing_imports():
    try:
        env.copy_graph(missing)
    except UnresolvedImportError:
        print(f"Import is unavailable: {missing}")
env.close()

Using a persistent environment

OntoEnv.connect(path) creates an empty managed environment if missing and reopens the saved catalog if present. It is a class method with a required path; OntoEnv.connect(".") uses the current directory. Source loading is explicit: use add or update when files or URLs should be read.

Keep the handle for the lifetime of the program. Context managers are optional:

from ontoenv import OntoEnv

env = OntoEnv.connect("./ontology-env")
try:
    site = env.add("./ontologies/site.ttl")
    graph = env.get_graph(site)
    closure, members = env.get_closure(site)

    # Capture one version when several reads must agree.
    with env.snapshot() as snapshot:
        graph = snapshot.view_graph(site)
        closure, members = snapshot.view_closure(site)
        dataset = snapshot.view_dataset(members)  # Preserves named contexts.
        editable = snapshot.copy_graph(site)

    # Keep using env in your application, and these results after the scope.
finally:
    env.close()  # Call this from the application's shutdown hook.

# Escaped views remain usable after env.close().
print(len(closure))

Live env.get_graph, get_closure, get_union and get_dataset calls remain supported. Each result captures the handle's selected version. Snapshot methods use paired view_* / copy_* names. Edits to a copy affect that independent object. The scope releases ownership only; there is no read transaction to commit. Managed writes publish before returning rather than at shutdown.

One writable managed handle holds the writer lock until close. Independent read-only workers can stay open while that updater publishes or restarts:

reader = OntoEnv.open("./ontology-env", read_only=True)
try:
    old = reader.snapshot()
    # After the updater process publishes a change:
    reader.refresh()  # Select saved state without fetching sources.
    current = reader.snapshot()
finally:
    reader.close()

# old and current, and their views, keep their selected versions.

Use reader.refresh() at a request boundary or on a timer. Request a new view after refresh to read the new selection. The service guide includes a framework lifespan example and the updater/reader process pattern. External stores retain their backend's concurrency and recovery guarantees.

When reopening, omitted settings retain their saved values. Explicit values, including False, "default" and [], override them:

with OntoEnv.connect("./ontology-env", strict=True, offline=True) as env:
    print(env.get_ontology_names())

with OntoEnv.connect(
    "./ontology-env", strict=False, offline=False,
    resolution_policy="default", search_directories=[],
) as env:
    print(env.get_ontology_names())

This also covers name validation, cache/TTL settings, and include/exclude filters. Writable opens persist overrides; read-only opens apply them only for that session. Reconfiguration does not re-ingest sources. Discovery changes take effect on the next update(). The old create_or_use_cached flag is removed in 0.7; use OntoEnv.connect(path).

For custom stores, sync="auto" reconnects quickly and reads only graphs the store can identify as changed. If the store cannot identify those graphs, OntoEnv asks for an explicit sync="full" instead of silently scanning everything. This synchronizes direct store changes; it does not check ontology files or URLs. Call env.update() after connecting when source files, remote sources, and their imports should be refreshed.

Most applications never need a different lifecycle method. create is useful for a setup command that must fail if the environment already exists. open is useful when deployment must have prepared the environment in advance and startup must neither create nor synchronize it. adopt explicitly reads every graph in a populated custom store once and records the ontology information OntoEnv needs; it does not fetch network imports. For tests and notebooks that should write no environment files, use OntoEnv(temporary=True).

Refreshing source files and URLs is separate from reconciling a custom graph store. env.update() checks changed local sources and expired remote sources, then follows their imports. env.update(force=True) forces every known source and its dependencies to be reread. To update one ontology and its imports, pass its file or URL directly; its stored graph is replaced automatically:

env.update(source)
env.update(source, force=True)  # reread even when the cached copy looks current

When graphs were changed directly in a custom store, use refresh_from_store() instead. A targeted refresh accepts exact graph IDs. To include the dependency closure currently known to OntoEnv:

report = env.refresh_from_store(graphs=env.list_closure(root))

If the external edit added an entirely new imported graph, use incremental refresh with a store that reports per-graph changes, or request refresh_from_store(full=True).

Namespace prefixes

OntoEnv can extract namespace prefix mappings from ontology source files. Prefixes come from both parser-level declarations (@prefix in Turtle, PREFIX in SPARQL-style syntaxes) and SHACL sh:declare entries.

# Get all namespaces across the entire environment
all_ns = env.get_namespaces()
# {'owl': 'http://www.w3.org/2002/07/owl#', 'brick': 'https://brickschema.org/schema/Brick#', ...}

# Get namespaces for a single ontology
ns = env.get_namespaces("https://brickschema.org/schema/1.4-rc1/Brick")

# Include namespaces from transitive owl:imports
ns_with_imports = env.get_namespaces("https://brickschema.org/schema/1.4-rc1/Brick", include_closure=True)

Prefixes you want every session to use can be saved in the environment. Saved bindings take precedence over the prefixes ontologies declare, and are applied to get_namespaces(), get_dataset(), copy_dataset(), and the views from get_closure() / get_union():

env.bind_namespace("bldg", "urn:my-building#")   # persisted in .ontoenv/ontoenv.json
env.get_bound_namespaces()                         # {'bldg': 'urn:my-building#'}
env.unbind_namespace("bldg")

dataset.bind(...) on a Dataset from get_dataset() is not saved (rdflib also calls bind itself for its defaults and auto-generated ns1-style prefixes), and refresh_dataset_from_env() resets the dataset's bindings to the env's.

From the CLI:

ontoenv namespaces                                     # all namespaces
ontoenv namespaces https://example.org/my-ontology     # single ontology
ontoenv namespaces https://example.org/my-ontology --closure   # with imports
ontoenv namespaces --json                              # JSON output
ontoenv config bind bldg urn:my-building#              # save a prefix binding
ontoenv config unbind bldg

Custom graph store

If you want OntoEnv to write graphs into an existing Python-backed store, pass a graph_store object that implements a small protocol:

class GraphStore:
    # Required
    def add_graph(self, iri: str, graph: Graph, overwrite: bool = False) -> None: ...
    def get_graph(self, iri: str) -> Graph: ...   # used for read-only views (get_*)
    def remove_graph(self, iri: str) -> None: ...
    def graph_ids(self) -> list[str]: ...

    # Optional
    def copy_graph(self, iri: str) -> Graph: ...  # used for mutable copies (copy_*)
                                                  # falls back to get_graph when absent
    def size(self) -> dict[str, int]: ...         # returns {"num_graphs": ..., "num_triples": ...}
    def store_state(self) -> dict[str, str]: ...  # {"id": opaque_id, "revision": opaque_revision}
    def graph_revisions(self) -> dict[str, str]: ... # opaque revision per graph IRI

copy_graph lets stores distinguish between returning a live view (get_graph) and a detached mutable copy (copy_graph). All copy_* methods (copy_graph, copy_closure, copy_union, copy_dataset) dispatch to copy_graph when it is present, and fall back to get_graph otherwise. The get_* methods always use get_graph.

Example:

store = DictGraphStore()
env = OntoEnv(graph_store=store, temporary=True)

For a persistent custom store, use connect:

env = OntoEnv.connect("./environment", graph_store=store)

If the store implements the optional change-reporting methods, OntoEnv can notice external edits and read only the affected graphs. Otherwise, writes through env remain synchronized automatically, while out-of-band edits require sync="full".

Temporary environments save no persistent ontology index. To scan a pre-populated temporary store without the removed init_from_store flag:

env = OntoEnv(graph_store=store, temporary=True)
report = env.refresh_from_store(full=True)
print(report.added)

RDFLib store with Rust SPARQL

If you want to use ontoenv as an rdflib store directly, use OntoEnvStore. This gives you normal rdflib.Graph and rdflib.Dataset objects, but executes SPARQL through the Rust backend instead of rdflib's Python query engine.

Use env.get_dataset() to get a read-only rdflib.Dataset view of the env. It uses the zero-copy rdf5d snapshot when a persistent .ontoenv/store.r5tu exists and otherwise falls back to an in-memory view. Use env.copy_dataset() when you need a mutable in-memory dataset.

from rdflib import URIRef
from ontoenv import OntoEnv

env = OntoEnv(path=".demo-env", recreate=True, offline=True, search_directories=["./brick"])
brick_name = env.add("./brick/Brick.ttl")
env.update()
env.flush()

dataset = env.get_dataset()

for row in dataset.query(
    """
    SELECT ?entity ?label
    WHERE {
      GRAPH <https://brickschema.org/schema/1.4/Brick> {
        ?entity <http://www.w3.org/2000/01/rdf-schema#label> ?label .
      }
    }
    LIMIT 5
    """
):
    print(row.entity, row.label)

brick_graph = dataset.graph(URIRef(brick_name))
print(len(brick_graph))
env.close()

Importing ontoenv also registers the rdflib plugin name "ontoenv", so this works too:

from rdflib import Graph
import ontoenv

graph = Graph(store="ontoenv")

See demo_rdflib_store.py for a complete runnable example.

CLI Entrypoint

Installing ontoenv also provides the Rust-backed ontoenv command-line tool:

pip install ontoenv
ontoenv --help

The CLI is identical to the standalone ontoenv-cli binary; see the top-level README for usage.

Metadata

Release files for ontoenv 0.7.0rc1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ontoenv 0.7.0rc1
File Size Uploaded Provenance Actions
ontoenv-0.7.0rc1.tar.gz 1.4 MB Without provenance Details for ontoenv-0.7.0rc1.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for ontoenv 0.7.0rc1
File Actions
ontoenv-0.7.0rc1-cp311-abi3-win_amd64.whl CPython 3.11 abi3 Windows x86-64 Details for ontoenv-0.7.0rc1-cp311-abi3-win_amd64.whl
ontoenv-0.7.0rc1-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.11 abi3 Linux glibc 2.17+ x86-64 Details for ontoenv-0.7.0rc1-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
ontoenv-0.7.0rc1-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.11 abi3 Linux glibc 2.17+ ARM64 Details for ontoenv-0.7.0rc1-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
ontoenv-0.7.0rc1-cp311-abi3-macosx_11_0_arm64.whl CPython 3.11 abi3 macOS 11.0+ ARM64 Details for ontoenv-0.7.0rc1-cp311-abi3-macosx_11_0_arm64.whl
ontoenv-0.7.0rc1-cp311-abi3-macosx_10_14_x86_64.whl CPython 3.11 abi3 macOS 10.14+ x86-64 Details for ontoenv-0.7.0rc1-cp311-abi3-macosx_10_14_x86_64.whl
ontoenv-0.7.0rc1-cp311-abi3-macosx_10_14_x86_64.macosx_11_0_arm64.macosx_10_14_universal2.whl CPython 3.11 abi3 macOS 10.14+ universal2 (ARM64, x86-64), macOS 10.14+ x86-64, macOS 11.0+ ARM64 Details for ontoenv-0.7.0rc1-cp311-abi3-macosx_10_14_x86_64.macosx_11_0_arm64.macosx_10_14_universal2.whl

Total release size: 53.1 MB

Release files / ontoenv-0.7.0rc1.tar.gz

Download URL ontoenv-0.7.0rc1.tar.gz
Size 1.4 MB
Tags Source
SHA-256 checksum
How to use checksums
1333e9c6cff9b74492c202afbbaf1ab2b13fa74f181473c7feec7eb054a7da61
BLAKE2b-256 checksum
How to use checksums
6b9f23fe364c535b4d00da30d377f9508b34d58fd22852de8cb55ed674892497
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / ontoenv-0.7.0rc1-cp311-abi3-win_amd64.whl

Download URL ontoenv-0.7.0rc1-cp311-abi3-win_amd64.whl
Size 7.0 MB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
f08108287b0f6752821ecf1e8224a9e04e3608b7460fb87e689320f830800238
BLAKE2b-256 checksum
How to use checksums
4c570ef603e1db93617a0d39ca6892e12c81c4291cff6491997356ae9882acbd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / ontoenv-0.7.0rc1-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL ontoenv-0.7.0rc1-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 7.4 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
022c245ec1c54dfd185218808961ce3a0e2afc30691d16f907bcf1552397c20e
BLAKE2b-256 checksum
How to use checksums
d6b1d6555d8cf6267cb902faa0f95beab1fa544d16434a654803af5fd0f94667
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / ontoenv-0.7.0rc1-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL ontoenv-0.7.0rc1-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 6.9 MB
Tags CPython 3.11 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
793f4fd6bc40be24492656010756c0ffefc97030a6b2d69478514d49ca4293bc
BLAKE2b-256 checksum
How to use checksums
2f3b83b30ebeb839cb95102df8b190bc57ab7293bf4e0b31f8d6fc9818949dc5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / ontoenv-0.7.0rc1-cp311-abi3-macosx_11_0_arm64.whl

Download URL ontoenv-0.7.0rc1-cp311-abi3-macosx_11_0_arm64.whl
Size 7.4 MB
Tags CPython 3.11 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
f60e8a5966bfba20b2fc0bb351dc41778e1bba1b385a580ed5cc1c1d5bb9ffac
BLAKE2b-256 checksum
How to use checksums
6d79740e5cfaad1654ef4f9b7135c045360975b1bb00da2d55a4b690dd149565
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / ontoenv-0.7.0rc1-cp311-abi3-macosx_10_14_x86_64.whl

Download URL ontoenv-0.7.0rc1-cp311-abi3-macosx_10_14_x86_64.whl
Size 7.8 MB
Tags CPython 3.11 abi3 macOS 10.14+ x86-64
SHA-256 checksum
How to use checksums
3402b5e28a0c019c7efe0f743484e104007a742ddb26d20a43a0cc6b6c648368
BLAKE2b-256 checksum
How to use checksums
b611f021cac37c5bec497322898957b2422773256a951e86921169e3ad8472da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / ontoenv-0.7.0rc1-cp311-abi3-macosx_10_14_x86_64.macosx_11_0_arm64.macosx_10_14_universal2.whl

Download URL ontoenv-0.7.0rc1-cp311-abi3-macosx_10_14_x86_64.macosx_11_0_arm64.macosx_10_14_universal2.whl
Size 15.2 MB
Tags CPython 3.11 abi3 macOS 10.14+ universal2 (ARM64, x86-64) macOS 10.14+ x86-64 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
08ab045cb3bc779ba31c2f3b5a267208141e851f73c05dc4b2ec602c1dcc92b3
BLAKE2b-256 checksum
How to use checksums
0f93164d1bdcb06a165cb5109b3d13932ec73cb29e59acdb62772b3ff885e069
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for ontoenv 0.7.0rc1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page