Neo4j Python Driver 4.0

The Official Neo4j Driver for Python.

Neo4j versions supported:

  • Neo4j 4.0

  • Neo4j 3.5

Python versions supported:

  • Python 3.8

  • Python 3.7

  • Python 3.6

  • Python 3.5

Note

Python 2.7 support has been dropped.

The previous driver Python Driver 1.7 supports older versions of python, the Neo4j 4.0 will work in fallback mode with that driver.

Quick Example

from neo4j import GraphDatabase

uri = "neo4j://localhost:7687"
driver = GraphDatabase.driver(uri, auth=("neo4j", "password"))

def create_friend_of(tx, name, friend):
    tx.run("CREATE (a:Person)-[:KNOWS]->(f:Person {name: $friend}) "
           "WHERE a.name = $name "
           "RETURN f.name AS friend", name=name, friend=friend)

with driver.session() as session:
    session.write_transaction(create_friend_of, "Alice", "Bob")

with driver.session() as session:
    session.write_transaction(create_friend_of, "Alice", "Carl")

driver.close()
from neo4j import GraphDatabase

uri = "neo4j://localhost:7687"
driver = GraphDatabase.driver(uri, auth=("neo4j", "password"))

def get_friends_of(tx, name):
    friends = []
    result = tx.run("MATCH (a:Person)-[:KNOWS]->(f) "
                         "WHERE a.name = $name "
                         "RETURN f.name AS friend", name=name):
    for record in result:
        friends.append(record["friend"])
    return friends

with driver.session() as session:
    friends = session.read_transaction(get_friends_of, "Alice")
    for friend in friends:
        print(friend)

driver.close()

Installation

To install the latest stable release, use:

python -m pip install neo4j

To install the latest pre-release, use:

python -m pip install --pre neo4j

Note

It is always recommended to install python packages for user space in a virtual environment.

Virtual Environment

To create a virtual environment named sandbox, use:

python -m venv sandbox

To activate the virtual environment named sandbox, use:

source sandbox/bin/activate

To deactivate the current active virtual environment, use:

deactivate