About the Driver

In the Cypher Fundamentals course, we cover how to query Neo4j using a language called Cypher. To execute a Cypher statement against a Neo4j database you will use an object called a Driver.

The Driver object is a thread-safe, application-wide fixture from which all Neo4j interaction derives.

The Driver API is topology independent, so you can run the same code against a Neo4j cluster or a single DBMS.

To connect to and query Neo4j from within a Python application, you use the Neo4j Python Driver.

The Neo4j Python Driver is one of five officially supported drivers, the others are Java, JavaScript, .NET, and Go. There are also a wide range of Community Drivers available for other languages including PHP and Ruby.

You should create a single instance of the Driver in your application per Neo4j cluster or DBMS, which can then be shared across your application.

Installing the Driver

The Neo4j Python Driver is available through the pip management system and can be installed with the pip command.

shell
pip install neo4j

Creating a Driver Instance

Each driver instance will connect to one DBMS, or Neo4j cluster, depending on the value provided in the connection string.

The neo4j package exports a GraphDatabase object. This object provides a driver() function for creating a new driver instance.

The driver() function requires one mandatory parameter, a connection string for the Neo4j cluster or DBMS - for example neo4j://localhost:7687 or neo4j+s://dbhash.databases.neo4j.io:7687.

Additionally, you will also pass a named parameter auth to represent the Neo4j user credentials. You can provide basic username/password authentication by passing the username and password as a tuple.

Here is an example for how to create a driver instance to connect to a Neo4j instance running on localhost on port 7687 with the username neo4j and password neo:

python
Creating a Driver Instance
# Import the neo4j dependency
from neo4j import GraphDatabase

# Create a new Driver instance
with GraphDatabase.driver(
    "neo4j://localhost:7687", 
    auth=("neo4j", "neo")
) as driver:
    # Execute code before the driver graciously closes itself

The above example creates an unencrypted connection to the Neo4j server at localhost on the default port number of 7687. The driver then attemps to authenticate against the server using a basic authentication with the username neo4j and password neo.

Verifying Connectivity

You can verify that the connection details used during driver instantiation are correct by calling the verifyConnectivity() function. This function will raise a Neo4jException with a code property of Neo.ClientError.Security.Unauthorized if a connection could not be made.

python
Verify Connectivity
# Verify the connection details
driver.verify_connectivity()

Check Your Understanding

1. Which of the following programming languages have officially supported drivers?

  • ✓ .NET

  • ✓ Go

  • ✓ Java

  • ✓ JavaScript

  • ✓ Python

  • ❏ PHP

  • ❏ Ruby

Hint

Five languages are officially supported by Neo4j.

Solution

The five supported languages are .NET, Go, Java, JavaScript and Python.

2. What name is the Neo4j Python Driver registered under in the Python Package Index?

  • GraphDatabase

  • neo4j

  • neo4j-driver

  • neo4j-python-driver

Hint

The package is registered under neo4j.

Solution

The package is registered under neo4j.

Lesson Summary

In this lesson, you learned about the Neo4j Python Driver and how it can be used to connect to Neo4j from within a Python application.

In the next lesson, we will take a closer look at the first argument in the GraphDatabase.driver() function, the connection string.