rdflib-shim
raw JSON → 1.0.3 verified Mon Apr 27 auth: no python
rdflib-shim is a compatibility layer that smooths over breaking changes between rdflib 5 and 6, allowing code written for rdflib 5 to work with rdflib 6 without modification. Version 1.0.3 (latest) requires Python >=3.7. The library is maintained but low-churn (last release 2022).
pip install rdflib-shim Common errors
error AttributeError: module 'rdflib' has no attribute 'RDF' ↓
cause In rdflib 6, the RDF namespace is no longer available under rdflib.RDF; it moved to rdflib.namespace.
fix
Add
import rdflib_shim before import rdflib to restore the old behavior. error AttributeError: module 'rdflib' has no attribute 'term' ↓
cause rdflib 6 changed internal module structure. Some direct references to rdflib.term fail.
fix
Use
from rdflib.term import * or import the shim first. error ImportError: cannot import name 'BNode' from 'rdflib' ↓
cause In rdflib 6, BNode is not directly exported from rdflib.__init__; it's in rdflib.term.
fix
Either use
from rdflib.term import BNode or install/import rdflib-shim. Warnings
gotcha The shim must be imported before any rdflib imports. If rdflib is imported first, the patching won't apply. ↓
fix Always put `import rdflib_shim` as the very first import in your module.
deprecated rdflib-shim may not be needed for new projects targeting rdflib 6+ directly. Its primary purpose is to ease migration from rdflib 5. ↓
fix For new code, consider writing directly against rdflib 6 APIs and skip the shim.
Imports
- rdflib_shim
import rdflib_shim
Quickstart
import rdflib_shim
from rdflib import Graph, URIRef, Literal
g = Graph()
g.parse('http://example.org/test.ttl', format='turtle')
for s, p, o in g:
print(s, p, o)