Pymantic
raw JSON → 1.0.1 verified Mon Apr 27 auth: no python
Pymantic is a Semantic Web and RDF library for Python, providing parsers, serializers, and SPARQL client support. The latest version is 1.0.1 (released 2024) with slow release cadence. It supports RDF/XML, Turtle, JSON-LD, TriG, and N-Triples formats.
pip install pymantic Common errors
error ImportError: cannot import name 'SPARQLClient' from 'pymantic' ↓
cause Old import path `from pymantic import SPARQLClient` no longer works after restructuring in v1.0.0.
fix
Use
from pymantic.sparql import SPARQLClient. error AttributeError: 'Graph' object has no attribute 'parse' ↓
cause `Graph.parse` method requires the graph to be created with a filename or explicitly parsed. In older versions, Graph had different API.
fix
Ensure you are using
from pymantic.io import Graph and call graph.parse('file', format='...'). For in-memory, create with Graph() then parse. Warnings
breaking Version 1.0.0 dropped Python 2.x support and restructured packaging. Import paths changed from old patterns like `from pymantic import sparql`. ↓
fix Use `from pymantic.sparql import SPARQLClient` etc. Update imports per new package structure.
deprecated The `lepl`-based parsers were replaced by `lark` in v0.2.0. Old code relying on `lepl` behavior may break. ↓
fix Ensure you use compatible syntax; lark may have stricter parsing. Update Turtle files if needed.
gotcha Parsing JSON-LD requires the `json-ld` optional dependency? Actually the library supports JSON-LD but may need `PyYAML` or extra care. Check documentation for exact parsing options (e.g., base URI). ↓
fix Install `PyYAML` if needed: `pip install PyYAML`. Use `graph.parse('file.jsonld', format='json-ld')`.
Imports
- SPARQLClient
from pymantic.sparql import SPARQLClient - Graph
from pymantic.io import Graph - RDFXMLParser
from pymantic.io import RDFXMLParser - TurtleParser
from pymantic.io import TurtleParser - JSONLDParser
from pymantic.io import JSONLDParser - TriGSerializer
from pymantic.io import TriGSerializer
Quickstart
from pymantic.io import Graph
from pymantic.sparql import SPARQLClient
# Create a graph and parse a Turtle file
graph = Graph()
graph.parse('example.ttl', format='turtle')
print(f"Parsed {len(graph.triples)} triples")
# Query a SPARQL endpoint
client = SPARQLClient('http://dbpedia.org/sparql')
results = client.query('SELECT * WHERE {?s ?p ?o} LIMIT 10')
for result in results['results']['bindings']:
print(result)