PROV Python Library

2.1.1 · active · verified Sun Apr 12

The `prov` library is a Python implementation of the W3C Provenance Data Model (PROV). It facilitates the creation, manipulation, and serialization/deserialization of provenance documents, supporting formats like PROV-JSON, PROV-XML, and PROV-O (RDF). The library is actively maintained, with its latest version being 2.1.1, and releases occur as needed to support newer Python versions and fix bugs.

Warnings

Install

Imports

Quickstart

This example demonstrates how to create a simple PROV document, define namespaces, add entities, activities, and agents, and link them with provenance relations. It then prints the PROV-N (PROV Notation) representation of the document. For JSON or XML serialization, additional dependencies and specific serializers are used.

import datetime
from prov.model import ProvDocument, Namespace, PROV_REC_NAMESPACE

# Create a new provenance document
doc = ProvDocument()

# Declare namespaces
doc.add_namespace('ex', 'http://example.org/')
doc.set_default_namespace('http://example.com/prov-example/')

# Declare entities, activities, agents
e1 = doc.entity('ex:entity1', {'prov:label': 'Example Entity 1'})
a1 = doc.activity('ex:activity1', datetime.datetime.now(), datetime.datetime.now(), {'prov:label': 'Example Activity 1'})
ag1 = doc.agent('ex:agent1', {'prov:label': 'Example Agent 1', PROV_REC_NAMESPACE['type']: PROV_REC_NAMESPACE['Person']})

# Establish relationships
doc.wasGeneratedBy(e1, a1, datetime.datetime.now())
doc.wasAssociatedWith(a1, ag1)

# Print the PROV-N representation
print(doc.get_provn())

# To serialize to PROV-JSON (requires the 'json' extra, installed with `pip install prov[json]`)
# from prov.serializers import provjson
# import json
# with open('example.json', 'w') as f:
#     provjson.ProvJSONSerializer(doc).serialize(f)

view raw JSON →