RDFLib is a Python library for working with RDF (Resource Description Framework), a simple yet powerful language for representing information. It provides a Graph data structure, parsers and serializers for various RDF formats (e.g., Turtle, N-Triples, RDF/XML, JSON-LD), and SPARQL query capabilities. RDFLib is actively maintained with frequent minor releases and major versions typically every 1-2 years.
pip install rdflibVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create an RDF Graph, define namespaces, add triples using URIRef, BNode, and Literal, and serialize the graph to the Turtle format. It's a fundamental example of how to represent and manage RDF data programmatically.
Review calls to `Graph.parse` or `Dataset.parse` that use `publicID`. If your data contains relative URIs, ensure the `publicID` is correctly set to define the base URI, or use `base` parameter explicitly if appropriate.
Consult the RDFLib documentation for `Dataset` to identify deprecated methods and their recommended alternatives. Update your code to use the newer, more consistent API to avoid breakage in future major versions.
Install RDFLib with the necessary extras, e.g., `pip install "rdflib[json-ld,sparql,isodate,lxml,rdf4j,graphdb]"`, or install individual missing packages as needed (e.g., `pip install isodate`). Refer to the documentation for specific feature requirements.
Ensure you are using RDFLib >= 7.1.3 if you target Python 3.8. It's generally recommended to use the latest patch release for your major version.
Ensure your RDF input string or file strictly adheres to the specified RDF format (e.g., Turtle, N-Triples, RDF/XML, JSON-LD) and is well-formed. Double-check the `format` argument passed to `g.parse()`.
Review and correct the syntax of your SPARQL query to ensure it conforms to the SPARQL 1.1 grammar. Common mistakes include missing keywords (like `SELECT` or `WHERE`), incorrect curly brace placement, or malformed prefixes.
Convert the `URIRef` or `Literal` object to a string using `str()` before concatenating it with other strings, or use f-strings which handle conversion automatically.
Install the `pyld` package using pip: `pip install pyld`. If you also need other optional parsers/serializers, check their respective documentation for required dependencies.