Build applications with Neo4j and Python

The Neo4j Python driver is the official library to interact with a Neo4j instance through a Python application.

At the hearth of Neo4j lies Cypher, the query language you use to interact with a Neo4j database. While this guide does not require you to be a seasoned Cypher querier, it is going to be easier to focus on the Python-specific bits if you already know some Cypher. For this reason, although this guide does also provide a gentle introduction to Cypher along the way, consider checking out Getting started → Cypher for a more detailed walkthrough of graph databases modelling and querying if this is your first approach. You may then apply that knowledge while following this guide to develop your Python application.


Install the Neo4j Python driver with pip:

pip install neo4j

Connect to the database

Connect to a database by creating a Driver object and providing a URL and an authentication token. Once you have a Driver instance, use the .verify_connectivity() method to ensure that a working connection can be established.

from neo4j import GraphDatabase

# URI examples: "neo4j://localhost", "neo4j+s://"
URI = "<URI for Neo4j database>"
AUTH = ("<Username>", "<Password>")

with GraphDatabase.driver(URI, auth=AUTH) as driver:

Query the database

Execute a Cypher statement with the method Driver.execute_query(). Do not hardcode or concatenate parameters: use placeholders and specify the parameters as keyword arguments.

# Get the name of all 42 year-olds
records, summary, keys = driver.execute_query(
    "MATCH (p:Person {age: $age}) RETURN AS name",

# Loop through results and do something with them
for person in records:

# Summary information
print("The query `{query}` returned {records_count} records in {time} ms.".format(
    query=summary.query, records_count=len(records),

Run your own transactions

For more advanced use-cases, you can take control of the transaction lifecycle. A transaction is a unit of work that is either committed in its entirety or rolled back on failure. Use the methods Session.execute_read() and Session.execute_write() to run managed transactions.

def match_person_nodes(tx, name_filter):
    result ="""
        MATCH (p:Person) WHERE STARTS WITH $filter
        RETURN AS name ORDER BY name
        """, filter=name_filter)
    return list(result)  # return a list of Record objects

with driver.session(database="neo4j") as session:
    people = session.execute_read(
    for person in people:
        print(  # obtain dict representation

Close connections and sessions

Unless you created them using the with statement, call the .close() method on all Driver and Session instances to release any resources still held by them.




A Long Term Support release is one guaranteed to be supported for a number of years. Neo4j 4.4 is LTS, and Neo4j 5 will also have an LTS version.


Aura is Neo4j’s fully managed cloud service. It comes with both free and paid plans.


Cypher is Neo4j’s graph query language that lets you retrieve data from the database. It is like SQL, but for graphs.


Awesome Procedures On Cypher (APOC) is a library of (many) functions that can not be easily expressed in Cypher itself.


Bolt is the protocol used for interaction between Neo4j instances and drivers. It listens on port 7687 by default.


Atomicity, Consistency, Isolation, Durability (ACID) are properties guaranteeing that database transactions are processed reliably. An ACID-compliant DBMS ensures that the data in the database remains accurate and consistent despite failures.

eventual consistency

A database is eventually consistent if it provides the guarantee that all cluster members will, at some point in time, store the latest version of the data.

causal consistency

A database is causally consistent if read and write queries are seen by every member of the cluster in the same order. This is stronger than eventual consistency.


The null marker is not a type but a placeholder for absence of value. For more information, see Cypher Manual — Working with null.


A transaction is a unit of work that is either committed in its entirety or rolled back on failure. An example is a bank transfer: it involves multiple steps, but they must all succeed or be reverted, to avoid money being subtracted from one account but not added to the other.


Backpressure is a force opposing the flow of data. It ensures that the client is not being overwhelmed by data faster than it can handle.

transaction function

A transaction function is a callback executed by an execute_read or execute_write call. The driver automatically re-executes the callback in case of server failure.


A Driver object holds the details required to establish connections with a Neo4j database.