Ontologies can define entities located in other namespaces. An example is Gene Ontology (GO): the ontology IRI is ‘’, but the IRI of GO entities are not of the form ‘’ but ‘’ (note the missing ‘go.owl#’).

Accessing entities defined in another namespace

These entities can be accessed in Owlready2 using a namespace. The get_namepace(base_iri) global function returns a namespace for the given base IRI.

The namespace can then be used with the dot notation, similarly to the ontology.

>>> # Loads Gene Ontology (~ 170 Mb), can take a moment!
>>> go = get_ontology("").load()

>>> print(go.GO_0000001) # Not in the right namespace

>>> obo = get_namespace("")

>>> print(obo.GO_0000001)

>>> print(obo.GO_0000001.iri)

>>> print(obo.GO_0000001.label)
['mitochondrion inheritance']

.get_namepace(base_iri) can also be called on an Ontology, for example:

>>> obo = go.get_namespace("")

Namespaces created on an Ontology can also be used for asserting facts and creating classes, instances,…:

>>> with obo:
>>>     class MyNewClass(Thing): pass # Create

Creating classes in a specific namespace

When creating a Class or a Property, the namespace attribute is used to build the full IRI of the Class, and to define in which ontology the Class is defined (RDF triples are added to this ontology). The Class IRI is equals to the namespace’s base IRI (base_iri) + the Class name.

An ontology can always be used as a namespace, as seen in Classes and Individuals (Instances). A namespace object can be used if you want to locate the Class at a different IRI. For example:

>>> onto      = get_ontology("")
>>> namespace = onto.get_namespace("")

>>> class Drug(Thing):
...     namespace = namespace

In the example above, the Drug Class IRI is “”, but the Drug Class belongs to the ‘’ ontology.

Owlready2 proposes 3 methods for indicating the namespace:

  • the ‘namespace’ Class attribute

  • the ‘with namespace’ statement

  • if not provided, the namespace is inherited from the first parent Class

The following examples illustrate the 3 methods:

>>> class Drug(Thing):
...     namespace = namespace

>>> with namespace:
...     class Drug(Thing):
...         pass

>>> class Drug2(Drug):
...     # namespace is implicitely Drug.namespace
...     pass

Modifying a class defined in another ontology

In OWL, an ontology can also modify a Class already defined in another ontology.

In Owlready2, this can be done using the ‘with namespace’ statement. Every RDF triples added (or deleted) inside a ‘with namespace’ statement goes in the ontology corresponding to the namespace of the ‘with namespace’ statement.

The following example creates the Drug Class in a first ontology, and then asserts that Drug is a subclass of Substance in a second ontology.

>>> onto1 = get_ontology("")
>>> onto2 = get_ontology("")

>>> with onto1:
...     class Drug(Thing):
...         pass

>>> with onto2:
...     class Substance(Thing):
...         pass

...     Drug.is_a.append(Substance)

Renaming entities defined in a namespace

Owlready has no direct support for renaming entities defined in a namespace that do not correspond to an ontology. However, a simple workaround is to create an ontology with the same base IRI as the namespace, change this ontology base iri (with Ontoloy.base_iri = …), and then destroy the ontology.