PyShEx

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

Python implementation of the Shape Expressions (ShEx) language, used for validating RDF data against shape schemas. Current version 0.8.1, requires Python >=3.6, supports rdflib 5 and 6. Release cadence is sparse (major updates every few months).

pip install pyshex
error ModuleNotFoundError: No module named 'antlr4'
cause ANTLR runtime is missing or wrong version.
fix
pip install 'antlr4-python3-runtime==4.9'
error AttributeError: 'Graph' object has no attribute 'triples'
cause rdflib 5+ changed the API; use 'g.triples((s,p,o))' instead of g.triples.
fix
Replace g.triples(...) with g.triples((subject, predicate, object))
error ImportError: cannot import name 'ShExEvaluator' from 'pyshex.evaluator'
cause Import path changed; ShExEvaluator is now at pyshex directly.
fix
Use: from pyshex import ShExEvaluator
error ValueError: Missing 'schema' parameter
cause ShExEvaluator requires 'schema' and 'rdf_graph' as keyword arguments.
fix
Call as ShExEvaluator(rdf_graph=..., schema=..., focus=...)
breaking In 0.8.0, rdflib 5/6 compatibility changes may break code relying on older rdflib triples/indexing.
fix Update rdflib to >=5.0.0 and check for changes in graph iteration.
deprecated ShExEvaluator API changed: the 'workspace' parameter is deprecated, use 'rdf_graph' and 'schema' directly.
fix Replace workspace=... with rdf_graph=..., schema=...
gotcha ANTLR runtime version must match the version used by pyshex (currently pinned to 4.9). Mismatched ANTLR versions cause parsing errors.
fix Ensure antlr4-python3-runtime==4.9 is installed: pip install 'antlr4-python3-runtime==4.9'

Basic usage: parse an RDF graph, define a ShEx schema, and evaluate whether a node conforms.

from pyshex import ShExEvaluator
from rdflib import Graph

g = Graph()
g.parse("example.ttl", format="turtle")

shapes = """PREFIX ex: <http://example.org/>
ex:UserShape {
  ex:name xsd:string ;
  ex:age xsd:integer
}"""

evaluator = ShExEvaluator(rdf_graph=g, schema=shapes, focus="http://example.org/user1")
result = evaluator.evaluate()
print(result)