Using Neo4j from Python
This guide provides an overview of how to connecting to Neo4j from Python.
You should be familiar with graph database concepts and the property graph model. You should have created an Neo4j AuraDB cloud instance, or installed Neo4j locally.
Neo4j and Python
Neo4j provides drivers which allow you to make a connection to the database and develop applications which create, read, update, and delete information from the graph.
Learn with GraphAcademy
Building Neo4j Applications with Python
In this free course, we walk through the steps to integrate Neo4j into your Python projects. You will learn about the Neo4j Python Driver, how sessions and transactions work and how to query Neo4j from an existing application.
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 was removed in the 2.0 release of the driver. |
pip install neo4j
from neo4j import GraphDatabase
class HelloWorldExample:
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.execute_write(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]
if __name__ == "__main__":
greeter = HelloWorldExample("bolt://localhost:7687", "neo4j", "password")
greeter.print_greeting("hello, world")
greeter.close()
Driver configuration
From Neo4j version 4.0 and onwards, the default encryption setting is off by default and Neo4j will no longer generate self-signed certificates.
This applies to default installations, installations through Neo4j Desktop and Docker images.
You can verify the encryption level of your server by checking the dbms.connector.bolt.enabled
key in neo4j.conf
.
Certificate Type | Neo4j Cluster | Neo4j Standalone Server | Direct Connection to Cluster Member |
---|---|---|---|
Unencrypted |
|
|
|
Encrypted with Full Certificate |
|
|
|
Encrypted with Self-Signed Certificate |
|
|
|
|
N/A |
N/A |
Please review your SSL Framework settings when going into production. If necessary, you can also generate certificates for Neo4j with Letsencrypt
Name |
Version |
Authors |
neo4j-driver |
5.22.0 |
The Neo4j Team |
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.