Owlready2
raw JSON → 0.50 verified Sat May 09 auth: no python
Owlready2 is a Python library for ontology-oriented programming, allowing users to load OWL 2.0 ontologies as Python objects, modify them, save them, and perform reasoning via the HermiT reasoner. It includes an optimized RDF quadstore. Current version is 0.50 (as of latest release) and it follows an irregular release cadence. Requires Python >=3.6.
pip install owlready2 Common errors
error ModuleNotFoundError: No module named 'owlready2' ↓
cause Owlready2 is not installed, or the wrong import path is used.
fix
Run
pip install owlready2 and use import owlready2. error AttributeError: module 'owlready2' has no attribute 'default_world' ↓
cause Import was not explicit; `default_world` is not automatically imported with `import owlready2`.
fix
Use
from owlready2 import default_world. error NameError: name 'Thing' is not defined ↓
cause Thing is not imported.
fix
Add
from owlready2 import Thing to your imports. error Owlready2_OwlException: Cannot define class outside of a `with ontology:` block ↓
cause Trying to define classes or properties without the `with onto:` context manager.
fix
Wrap class definitions inside
with onto:. error AttributeError: 'Ontology' object has no attribute 'save' (or similar) ↓
cause Possibly using an outdated version where save syntax is different.
fix
Update to latest version and use
onto.save(). Warnings
gotcha The default_world is global; modifying it can affect other parts of the program. Use separate World() instances for isolation. ↓
fix Use `world = World()` and `onto = world.get_ontology(...)` instead of relying on the default world.
breaking Owlready2 changed the import path from the deprecated Owlready (no '2') in older versions. Code using `import owlready` will break. ↓
fix Use `import owlready2` and `from owlready2 import ...`
gotcha Modifying ontology entities (e.g., adding properties) must be done inside a `with onto:` block. Otherwise, changes may not persist or cause errors. ↓
fix Always use `with onto: ...` when defining classes or properties.
deprecated The `sync_reasoner()` function is deprecated in favor of using the HermiT reasoner directly with `sync_reasoner_hermit()`. However, `sync_reasoner()` still works for now. ↓
fix Use `sync_reasoner_hermit()` instead.
gotcha Installing Owlready2 may fail on some systems due to missing C++ build tools required for the quadstore (cysignals). Ensure you have a C++ compiler installed. ↓
fix Install build tools: on Windows install Microsoft C++ Build Tools; on Linux install g++ and python3-dev.
Imports
- default_world wrong
import owlready2correctfrom owlready2 import default_world - World
from owlready2 import World - get_ontology
from owlready2 import get_ontology - sync_reasoner
from owlready2 import sync_reasoner - Thing
from owlready2 import Thing - Property
from owlready2 import Property
Quickstart
from owlready2 import get_ontology, sync_reasoner, Thing, Property, default_world
# Load an ontology from a URL or local file
onto = get_ontology("http://www.semanticweb.org/owl/owlapi/tutorial/people.owl").load()
# Create new classes and properties
with onto:
class Person(Thing):
pass
class WorksFor(Property):
domain = [Person]
range = [Person]
# Create individuals
john = Person("John")
mary = Person("Mary")
john.WorksFor = [mary]
# Save ontology
onto.save()
# Run reasoning
sync_reasoner()
# Print all individuals
for person in onto.Person.instances():
print(person.name)