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… Learn 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 master v3 TM


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.

pip install neo4j-driver==1.0.0
from neo4j.v1 import GraphDatabase, basic_auth



  driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "password"))

  session = driver.session()



  session.run("CREATE (a:Person {name:'Arthur', title:'King'})")



  result = session.run("MATCH (a:Person) WHERE a.name = 'Arthur' RETURN a.name AS name, a.title AS title")

  for record in result:

      print("%s %s" % (record["title"], record["name"]))



  session.close()

Name

Version

Authors

neo4j-driver

1.0.0

The Neo4j Team, Nigel Small

Package

StackOverflow

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 reponsibility 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.

Both Python 2 and Python 3 are supported by most of the libraries listed here and this should help give more flexibility when putting together your Neo4j-based technology stack.

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. The core library has no external dependencies and 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-2.0

Docs

http://py2neo.org/

Python

2.7 / 3.3 / 3.4

Installation
pip install py2neo
Example
from py2neo import Graph, Path

graph = Graph()



tx = graph.cypher.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()]



friends = Path(alice, "KNOWS", bob, "KNOWS", carol)

graph.create(friends)

Neo4jRestClient

The main goal of neo4j-rest-client was to enable Python programmers already using Neo4j locally through python-embedded, to use the Neo4j REST server. So the syntax of neo4j-rest-client’s API is fully compatible with python-embedded. However, a new syntax is introduced in order to reach a more pythonic style and to enrich the API with the new features the Neo4j team introduces.

Author

Javier de la Rosa

Package

https://pypi.python.org/pypi/neo4jrestclient/

Source

https://github.com/versae/neo4j-rest-client

Example

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

Docs

http://neo4j-rest-client.readthedocs.org/

Bulbflow

blubflow.200x200

Bulbs is an open-source Python persistence framework for graph databases and the first piece of a larger web development toolkit. It’s like an ORM for graphs.

Author

James Thornton

Package

https://pypi.python.org/pypi/bulbs

Source

https://github.com/espeed/bulbs

Docs

http://bulbflow.com/

Python

2.6 / 2.7 / 3.0 / 3.1 / 3.2

NeoModel

neomodel.200x80

An Object Graph Mapper (OGM) also usable for Django for the Neo4j graph database.

Author

Robin Edwards

Package

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

Source

http://github.com/robinedwards/neomodel

Docs

http://neomodel.readthedocs.org/

Python

2.7 / 3.4 / pypy / pypy3