Neo4j Python Driver 6.0¶
The Official Neo4j Driver for Python.
Bolt protocol versions supported:
Bolt 6.0
Bolt 5.0 - 5.8
Bolt 4.4
See https://7687.org/bolt-compatibility/ for what Neo4j DBMS versions support which Bolt versions. See https://neo4j.com/developer/kb/neo4j-supported-versions/ for a driver-server compatibility matrix.
Python versions supported:
Python 3.13
Python 3.12
Python 3.11
Python 3.10
Topics¶
Installation¶
Note
neo4j-driver is the old name for this package. It is now deprecated and
and will receive no further updates starting with 6.0.0. Make sure to
install neo4j as shown here.
Note
It is always recommended to install python packages for user space in a virtual environment.
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
Alternative Installation for Better Performance¶
You may want to have a look at the available Rust extensions for this driver for better performance. The Rust extensions are not installed by default. For more information, see neo4j-rust-ext.
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
Development Environment¶
For development, we recommend to run Python in development mode (python -X dev ...).
Specifically for this driver, this will:
enable
ResourceWarning, which the driver emits if resources (e.g., Sessions) aren’t properly closed.enable
DeprecationWarning, which the driver emits if deprecated APIs are used.enable the driver’s debug mode (this can also be achieved by setting the environment variable
PYTHONNEO4JDEBUG):
the driver will raise an exception if non-concurrency-safe methods are used concurrently.
the driver will emit warnings if the server sends back notification (see also warn_notification_severity).
Added in version 5.15.
Changed in version 5.21: Added functionality to automatically emit warnings on server notifications.
Changed in version 6.0: Stabilized from preview.
Quick Example¶
from neo4j import GraphDatabase, RoutingControl
URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "password")
def add_friend(driver, name, friend_name):
driver.execute_query(
"MERGE (a:Person {name: $name}) "
"MERGE (friend:Person {name: $friend_name}) "
"MERGE (a)-[:KNOWS]->(friend)",
name=name, friend_name=friend_name, database_="neo4j",
)
def print_friends(driver, name):
records, _, _ = driver.execute_query(
"MATCH (a:Person)-[:KNOWS]->(friend) WHERE a.name = $name "
"RETURN friend.name ORDER BY friend.name",
name=name, database_="neo4j", routing_=RoutingControl.READ,
)
for record in records:
print(record["friend.name"])
with GraphDatabase.driver(URI, auth=AUTH) as driver:
add_friend(driver, "Arthur", "Guinevere")
add_friend(driver, "Arthur", "Lancelot")
add_friend(driver, "Arthur"