Goals This guide provides an overview of how to connecting to Neo4j from Python. While it is not comprehensive, it aims to introduce the available drivers and links to other relevant resources. Prerequisites You should be familiar with graph database… Read more →

Goals
This guide provides an overview of how to connecting to Neo4j from Python. While it is not comprehensive, it aims to introduce the available drivers and links to other relevant resources.
Prerequisites
Intermediate

python logo


Neo4j can be installed on any system and then accessed via its binary and HTTP APIs.

You can use the official binary driver for Python (neo4j-python-driver) or connect via HTTP with any of our community drivers.

Neo4j Python Driver

The Neo4j Python driver is officially supported by Neo4j and connects to the database using the binary protocol. It aims to be minimal, while being idiomatic to Python.

Support for Python 2 will be removed in the upcoming 2.0 release of the driver.
pip install neo4j
from neo4j import GraphDatabase

class HelloWorldExample(object):

    def __init__(self, uri, user, password):
        self._driver = GraphDatabase.driver(uri, auth=(user, password))

    def close(self):
        self._driver.close()

    def print_greeting(self, message):
        with self._driver.session() as session:
            greeting = session.write_transaction(self._create_and_return_greeting, message)
            print(greeting)

    @staticmethod
    def _create_and_return_greeting(tx, message):
        result = tx.run("CREATE (a:Greeting) "
                        "SET a.message = $message "
                        "RETURN a.message + ', from node ' + id(a)", message=message)
        return result.single()[0]

Name

Version

Authors

neo4j-driver

1.7.1

The Neo4j Team, Nigel Small

Package

Neo4j Online Community

Docs

API

Source

The Example Project

The Neo4j example project is a small, one page webapp for the movies database built into the Neo4j tutorial. The front-end page is the same for all drivers: movie search, movie details, and a graph visualization of actors and movies. Each backend implementation shows you how to connect to Neo4j from each of the different languages and drivers.

You can learn more about our small, consistent example project across many different language drivers here. You will find the implementations for all drivers as individual GitHub repositories, which you can clone and deploy directly.

Neo4j Community Drivers

The drivers below have been thankfully contributed by the Neo4j community. Many of these are fully featured and well maintained. But we don’t take any responsibility for their fitness for use with the most recent versions of Neo4j.

For anyone working with Python, the Neo4j community have contributed a range of driver options. These range from lightweight to comprehensive driver packages as well as libraries designed for use with web frameworks such as Django.

While Python 3 is preferred, some drivers still support Python 2, please check with the individual project if you need it.

While we do not provide a specific web framework recommendation, both the lightweight Flask and the more comprehensive Django frameworks are known to work well.

Many Pythonistas have invested a lot of time and love to develop these libraries, so if you use them, please provide feedback to the authors and help us improve.

Py2neo

py2neo.200x200

Py2neo is a client library and comprehensive toolkit for working with Neo4j from within Python applications and from the command line. It has been carefully designed to be easy and intuitive to use.

Author

Nigel Small

Package

https://pypi.python.org/pypi/py2neo

Source

https://github.com/nigelsmall/py2neo

Example

https://github.com/neo4j-examples/movies-python-py2neo

Docs

http://py2neo.org/

Python

2.7 / 3.4+

Protocols

Bolt, Http

Installation
pip install py2neo
Example
from py2neo import Graph
graph = Graph()

tx = graph.begin()
for name in ["Alice", "Bob", "Carol"]:
    tx.append("CREATE (person:Person {name:{name}}) RETURN person", name=name)
alice, bob, carol = [result.one for result in tx.commit()]

Neomodel

neomodel.200x80

An Object Graph Mapper built on top of the Neo4j python driver. Familiar Django style node definitions with a powerful query API, thread safe and full transaction support. A Django plugin django_nemodel is also available.

Author

Athanasios Anastasiou and Robin Edwards

Package

https://pypi.python.org/pypi/neomodel

Source

http://github.com/neo4j-contrib/neomodel

Docs

http://neomodel.readthedocs.org/

Python

2.7 / 3.3+

Protocols

Bolt

Example
from neomodel import StructuredNode, StringProperty, RelationshipTo, RelationshipFrom, config

config.DATABASE_URL = 'bolt://neo4j:test@localhost:7687'

class Book(StructuredNode):
    title = StringProperty(unique_index=True)
    author = RelationshipTo('Author', 'AUTHOR')

class Author(StructuredNode):
    name = StringProperty(unique_index=True)
    books = RelationshipFrom('Book', 'AUTHOR')

harry_potter = Book(title='Harry potter and the..').save()
rowling =  Author(name='J. K. Rowling').save()
harry_potter.author.connect(rowling)