PySHACL is a Python library that implements the W3C Shapes Constraint Language (SHACL) specification. It allows users to validate RDF data graphs against SHACL shapes, identify data quality issues, and generate detailed validation reports. Currently at version 0.31.0, the library maintains an active release cadence, with significant updates and fixes appearing every 1-3 months.
pip install pyshaclVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to validate a simple RDF data graph against a SHACL shapes graph using `pyshacl.validate`. It defines two in-memory graphs, one containing sample data and another defining a shape that requires every `ex:Person` to have exactly one `ex:name`. The validation identifies `ex:Jane` as non-conformant.
Upgrade Python to 3.8.1 or newer. For existing projects, pin PySHACL to `<0.25.0`.
Ensure your RDFLib installation is `rdflib>=6.3.2,<8.0.0` (or the latest compatible version) via `pip install --upgrade rdflib`.
Upgrade to PySHACL v0.28.1 or newer to avoid unintended logging side effects. If unable to upgrade, consider configuring your logger *after* PySHACL initialization or using a named logger instead of the root logger.
Review existing code that passes multiple data graphs. If independent validation per graph is desired, refactor to use `pyshacl.validate_each()` or loop through graphs calling `pyshacl.validate()` individually. Otherwise, be aware of the new combined dataset behavior.
pip install pyshacl
pip install rdflib
Verify the syntax of your RDF file (e.g., Turtle, RDF/XML) and ensure the specified `data_graph_format` or `shacl_graph_format` parameter (or CLI option) matches the actual file format.
Ensure the path to the shapes file is correct and accessible, and that the file contains valid SHACL syntax in the specified RDF format.