The Neo4j Python Driver is the official client library for interacting with Neo4j graph databases from Python applications. It provides a robust, high-performance, and idiomatic API for executing Cypher queries, managing sessions, and handling transactions. Version 6.1.0 is the latest stable release, with frequent updates and major versions released every few months, alongside Long Term Support (LTS) versions for stability.
pip install neo4jVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to connect to a Neo4j database, execute a write query using `execute_write`, and then a read query. It uses `os.environ.get` for secure credential handling and ensures the driver is closed properly.
Pass a `Config` object to the driver: `GraphDatabase.driver(uri, config=Config(encrypted=True, trust_strategy=TRUST_ALL_CERTIFICATES))`.
Update transaction methods: `session.execute_read(my_read_function)` and `session.execute_write(my_write_function)`.
Instead of `auth=AuthToken('basic', 'user', 'pass')`, use `from neo4j import basic_auth; auth=basic_auth('user', 'pass')`.Always ensure `driver.close()` is called, typically in a `finally` block or by using a context manager where applicable (e.g., `with driver.session() as session:` for sessions, but the driver itself needs explicit `close`).
Use `bolt://<host>:<port>` (default 7687) for direct connections or `neo4j://<host>:<port>` for routing capabilities with a Causal Cluster. For example: `bolt://localhost:7687`.
Double-check the credentials (username and password) passed to `basic_auth` or the `auth` parameter of `GraphDatabase.driver`.
Verify the Neo4j server is running and accessible from the client machine. Check firewall rules. Ensure the URI uses `bolt://` or `neo4j://` with the correct host and port (default 7687).
Ensure `driver` is assigned before use. Wrap connection logic in `try...except` and place `driver.close()` in a `finally` block. Use `with driver.session() as session:` for robust session management.
Update your import statement to `from neo4j import GraphDatabase`.