PyOxigraph
PyOxigraph provides Python bindings for Oxigraph, a high-performance SPARQL database and RDF toolkit implemented in Rust. It offers capabilities for parsing, storing, querying, and manipulating RDF data, including support for various RDF serialization formats and the SPARQL query language. The library is actively maintained with frequent releases, typically every few weeks, incorporating new features and performance improvements. Current version is 0.5.6.
Common errors
-
ModuleNotFoundError: No module named 'pyoxigraph'
cause The pyoxigraph library is not installed in your current Python environment.fixRun `pip install pyoxigraph` to install the library. -
error: C++ compiler support for C++20 is required
cause During installation (especially if pre-built wheels are unavailable), the underlying RocksDB dependency failed to compile because your C++ compiler does not support the C++20 standard.fixUpgrade your C++ compiler. For example, use GCC version 10 or newer, Clang version 11 or newer, or MSVC 19.28 (Visual Studio 2019 version 16.8) or newer. -
ImportError: /lib64/libc.so.6: version 'GLIBC_2.28' not found (required by .../pyoxigraph.cpython-3x-xxxx.so)
cause You are trying to run a pre-built pyoxigraph wheel on a Linux system with a Glibc version older than 2.28.fixUpgrade your Linux distribution to one that provides Glibc 2.28 or newer (e.g., Ubuntu 20.04+, Debian 10+). Alternatively, you can try to build `pyoxigraph` from source, which will link against your system's Glibc, but requires a Rust toolchain and C++20 compiler.
Warnings
- breaking The `rdf-star` feature (for RDF-star syntax) was replaced by `rdf-12` and `sparql-12` features in v0.5.0, aligning with W3C working drafts for RDF 1.2 and SPARQL 1.2. The triple term syntax also slightly changed.
- gotcha Starting from v0.5.2, PyOxigraph's underlying RocksDB dependency requires a C++20 compatible compiler to build from source. This primarily affects users installing `pyoxigraph` without pre-built wheels or on platforms where Rust's build system needs to compile RocksDB.
- gotcha Linux Python wheels for PyOxigraph, starting from v0.5.2, are built with a dependency on Glibc 2.28 or newer. This can cause runtime errors on older Linux distributions.
- gotcha For complex SPARQL queries, especially those involving `FROM` or `FROM NAMED` clauses, the `QueryEvaluator.prepare` method (introduced in v0.5.1) is the recommended approach over direct `QueryEvaluator.execute`. Using `QueryEvaluator.execute` might not correctly account for `FROM` clauses.
Install
-
pip install pyoxigraph
Imports
- Store
from pyoxigraph import Store
- NamedNode
from pyoxigraph import NamedNode
- Literal
from pyoxigraph import Literal
- Triple
from pyoxigraph import Triple
- QueryEvaluator
from pyoxigraph import QueryEvaluator
Quickstart
from pyoxigraph import Store, NamedNode, Literal, Triple
# Create an in-memory store
store = Store()
# Define some RDF terms
ex_s = NamedNode("http://example.com/subject")
ex_p = NamedNode("http://example.com/predicate")
ex_o = Literal("object value")
# Add a triple to the store
store.add(Triple(ex_s, ex_p, ex_o))
# Add another triple
store.add(Triple(NamedNode("http://example.com/another"), NamedNode("http://example.com/knows"), ex_s))
# Query the store using SPARQL
print("\n--- Query Results ---")
results = store.query("SELECT ?s ?p ?o WHERE { ?s ?p ?o }")
for s, p, o in results:
print(f"Subject: {s}, Predicate: {p}, Object: {o}")
# Check if the store contains a specific triple
if store.contains(Triple(ex_s, ex_p, ex_o)):
print(f"\nStore contains: {ex_s} {ex_p} {ex_o}")