Registry / database / owlrl
library7.1.4pypypi✓ verified 85d ago

OWL-RL is a Python library that provides a simple implementation of the OWL2 RL Profile, as well as basic RDFS inference, on top of RDFLib. It performs mechanical forward chaining to compute the deductive closure of RDF graphs. The current version is 7.1.4, and it is actively maintained with a focus on semantic web inference capabilities.

pip install owlrl
INSTALL
IMPORT
SIG · OWLRL
O
owlrl
databasepythonv7.1.4
Install
2.0s avg
Import
388ms
Disk
22MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v7.1.4 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.402s · 23.5MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 2.0s · import 0.375s · 24MB
22MB installed
● package 22MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

DeductiveClosure
from owlrl import DeductiveClosure
from RDFClosure import DeductiveClosure
The module was renamed from RDFClosure to owlrl in version 5.1.0. Ensure you're using the correct module name for newer versions.
RDFS_Semantics
from owlrl import RDFS_Semantics
OWLRL_Semantics
from owlrl import OWLRL_Semantics
Graph
from rdflib import Graph

This quickstart demonstrates how to create an RDFLib graph, populate it with some triples, and then use `owlrl.DeductiveClosure` with `OWLRL_Semantics` or `RDFS_Semantics` to infer new triples based on the respective OWL 2 RL or RDFS rules. The `expand` method modifies the graph in place, adding all possible derived triples. It shows how to check for an inferred triple after expansion.

from rdflib import Graph, Literal, Namespace, RDF, RDFS from owlrl import DeductiveClosure, OWLRL_Semantics # Define a simple namespace for demonstration ex = Namespace("http://example.org/ontology#") # Create an RDFLib graph graph = Graph() graph.bind("ex", ex) # Add some initial triples (asserted facts) graph.add((ex.Person, RDF.type, RDFS.Class)) graph.add((ex.Student, RDFS.subClassOf, ex.Person)) graph.add((ex.John, RDF.type, ex.Student)) # Add a rule that says if someone is a Person, they are also an Agent graph.add((ex.Agent, RDF.type, RDFS.Class)) graph.add((ex.Person, RDFS.subClassOf, ex.Agent)) print(f"Graph size before OWL RL expansion: {len(graph)}") # Initialize and run the OWL 2 RL deductive closure closure = DeductiveClosure(OWLRL_Semantics) closure.expand(graph) # The graph now contains inferred triples, e.g., John is a Person, and also an Agent print(f"Graph size after OWL RL expansion: {len(graph)}") # Check for an inferred triple if (ex.John, RDF.type, ex.Agent) in graph: print("Inferred: John is an Agent.") # You can also run RDFS inference separately rdfs_graph = Graph() rdfs_graph.bind("ex", ex) rdfs_graph.add((ex.Teacher, RDFS.subClassOf, ex.Person)) rdfs_graph.add((ex.Jane, RDF.type, ex.Teacher)) print(f"\nGraph size before RDFS expansion: {len(rdfs_graph)}") from owlrl import RDFS_Semantics rdfs_closure = DeductiveClosure(RDFS_Semantics) rdfs_closure.expand(rdfs_graph) print(f"Graph size after RDFS expansion: {len(rdfs_graph)}") if (ex.Jane, RDF.type, ex.Person) in rdfs_graph: print("Inferred: Jane is a Person.")
Debug
Known issues
breakingOWL-RL version 7.x, following RDFLib's dependency updates, now requires Python 3.9 or higher. Older projects using Python 2.7 or earlier Python 3 versions (e.g., 3.5, 3.6) will need to upgrade their Python environment or use an older `owlrl` version.
fix
Upgrade Python to 3.9+ or pin `owlrl` to an older version compatible with your Python environment (e.g., `owlrl<7.0.0` for Python 3.5-3.8, or `owlrl<5.0.0` for Python 2.7).
affects: >=7.0.0
breakingIn version 5.1.0, the primary module for the library was renamed from `RDFClosure` to `owlrl`. If you are migrating an older codebase, import statements like `from RDFClosure import DeductiveClosure` must be updated to `from owlrl import DeductiveClosure`.
fix
Update your import statements to use `from owlrl import ...` instead of `from RDFClosure import ...`.
affects: >=5.1.0
gotchaThe `DeductiveClosure` class does not automatically interpret `owl:imports` statements. Any imported ontologies must be explicitly loaded and merged into the graph (e.g., using `owlrl.interpret_owl_imports`) before calling `expand()` for the inference process to consider them.
fix
Pre-process your graph to interpret `owl:imports` statements, for example:
```python
from rdflib import Graph
from owlrl import interpret_owl_imports

g = Graph().parse('my_ontology_with_imports.owl')
interpret_owl_imports('auto', g)
# Now proceed with DeductiveClosure().expand(g)
```
affects: All versions
gotchaRDFLib's default datatype handling may not meet the stricter requirements of OWL 2 RL, potentially leading to incorrect inferences for complex datatypes. `owlrl` provides improved datatype conversion routines, which can be explicitly enabled.
fix
To use the stricter datatype conversions, call `DeductiveClosure.use_improved_datatypes_conversions()` before expanding graphs, or initialize `DeductiveClosure(improved_datatypes=True)`.
```python
from owlrl import DeductiveClosure, OWLRL_Semantics

DeductiveClosure.use_improved_datatypes_conversions() # Apply globally
# or: closure = DeductiveClosure(OWLRL_Semantics, improved_datatypes=True)
```
affects: All versions
gotchaA known bug in RDFLib's Turtle parser can cause exceptions when parsing abbreviated double datatypes (e.g., `1.234E56`). It's advisable to avoid this specific literal syntax in your Turtle files if you encounter parsing errors.
fix
Represent double datatypes in their full lexical form (e.g., `"1.234E56"^^xsd:double`) or use a different serialization format if this issue is critical.
affects: All versions (due to underlying RDFLib issue)
gotchaOn Windows systems, if both the `owlrl` package and an `owlrl.py` command-line script are present and accessible in `PYTHON_PATH`, attempting to `from owlrl import convert_graph` (an older entry point) could lead to an import collision, as Python might incorrectly try to import from the script file.
fix
Avoid using the `convert_graph` entry point if encountering this issue. Ensure your Python environment's `PYTHON_PATH` doesn't lead to ambiguity, or consider upgrading `owlrl` which has renamed scripts to avoid `.py` extensions.
affects: <7.x (likely mitigated by script renaming in newer versions)
Errors
Common errors & fixes
cannot import name 'convert_graph' from 'owlrl'
This error often occurs on Windows systems when a local script or a file within the virtual environment is named `owlrl.py`, causing Python to try importing `convert_graph` from the script file itself instead of the actual installed `owlrl` package.
fix
Rename the conflicting `owlrl.py` file to something else (e.g., `my_script.py`) or ensure that `owlrl` is not present in your `PYTHON_PATH` in a way that shadows the installed package.
ImportError: cannot import name 'RDFXML' from 'RDFClosure.LCC'
This error happens in `owlrl` versions 6.x when running the command-line utility (e.g., `owlrl --help`). It's due to the `owlrl.py` script attempting to import outdated format constants (like `RDFXML`) that have been removed or moved in newer `rdflib` or `RDFClosure` versions.
fix
This is a bug within older `owlrl` command-line scripts (versions 6.x). If you encounter this, either update `owlrl` to a newer version that has fixed this script (if available) or use the `owlrl` library programmatically in Python code, avoiding the command-line script.
owlrl relies on OWL.Datatype which does not exist in the OWL namespace in rdflib
This indicates an incompatibility where `owlrl` is trying to reference `OWL.Datatype` from `rdflib`'s OWL namespace, but this specific constant or structure is not present or has changed in the `rdflib` version being used.
fix
Ensure that your `rdflib` and `owlrl` installations are compatible. This might involve updating both libraries to their latest stable versions, or, if issues persist, checking the `owlrl` GitHub issues for specific `rdflib` version requirements or workarounds.
Upgrade
Version history
7.1.4latest on PyPI · released Jul 29, 2025
Audit
Dependencies
rdflibrequiredCore dependency for RDF graph representation and manipulation, required >=7.1.4.
Agent activity
4 hits · last 30 days
node
4
Resources