Overview of Grakn Python Driver

Grakn Python Client


The Python client requires Python 3.6.

To install it, simply run:

$ pip install grakn

You will also need access to a Grakn database. Head here to see how to set up a Grakn database.


Begin by importing the client:

>>> import grakn

Now you can connect to a knowledge base:

>>> client = grakn.Client(uri='http://localhost:4567', keyspace='mykb')

You can write to the knowledge base:

>>> client.execute('define person sub entity;')
>>> client.execute('define name sub attribute, datatype string;')
>>> client.execute('define person has name;')
>>> client.execute('insert $bob isa person, has name "Bob";')
[{'bob': {'type': {'label': 'person', '@id': '/kb/mykb/type/person'}, 'id': ...}}]

Or read from it:

>>> client.execute('match $bob isa person, has name $name; get $name;')
[{'name': {'type': {'label': 'name', '@id': '/kb/mykb/type/name'}, 'value': 'Bob', 'id': ...}}]

You can also configure inference or support for multiple queries:

>>> resp = client.execute('match $x isa person; get; match ($y, $z) isa marriage; get;', infer=False, multi=True)