{"id":1580,"library":"neo4j","title":"Neo4j Python Driver","description":"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.","status":"active","version":"6.1.0","language":"python","source_language":"en","source_url":"https://github.com/neo4j/neo4j-python-driver","tags":["database","graph","neo4j","driver"],"install":[{"cmd":"pip install neo4j","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required for timezone-aware date/time handling within the driver.","package":"pytz"},{"reason":"Required for OAuth-based authentication schemes (e.g., bearer authentication).","package":"requests-oauthlib"}],"imports":[{"symbol":"GraphDatabase","correct":"from neo4j import GraphDatabase"},{"symbol":"basic_auth","correct":"from neo4j import basic_auth"},{"symbol":"ClientError","correct":"from neo4j.exceptions import ClientError"},{"note":"The `neo4j.v1` module was for the older V1 driver and is now deprecated and removed. Always import directly from `neo4j`.","wrong":"from neo4j.v1 import GraphDatabase","symbol":"GraphDatabase (old)","correct":"from neo4j import GraphDatabase"}],"quickstart":{"code":"import os\nfrom neo4j import GraphDatabase, basic_auth\n\n# Replace with your Neo4j URI and credentials\nNEO4J_URI = os.environ.get(\"NEO4J_URI\", \"bolt://localhost:7687\")\nNEO4J_USERNAME = os.environ.get(\"NEO4J_USERNAME\", \"neo4j\")\nNEO4J_PASSWORD = os.environ.get(\"NEO4J_PASSWORD\", \"password\")\n\ndriver = None\ntry:\n    # Use basic_auth for username/password. For 5.x and below, this was `auth=(username, password)`.\n    # In 6.x, `auth` parameter still exists, but `AuthTokens` (or `basic_auth`) is the explicit way.\n    driver = GraphDatabase.driver(\n        NEO4J_URI,\n        auth=basic_auth(NEO4J_USERNAME, NEO4J_PASSWORD)\n    )\n    driver.verify_connectivity()\n\n    with driver.session() as session:\n        greeting = session.execute_write(\n            lambda tx: tx.run(\n                \"CREATE (a:Greeting) SET a.message = $message RETURN a.message + ', from node ' + id(a)\",\n                message=\"Hello, World\"\n            ).single().value()\n        )\n        print(greeting)\n\n        # Example of a read query\n        result = session.run(\"MATCH (a:Greeting) RETURN a.message LIMIT 1\")\n        for record in result:\n            print(f\"Found greeting: {record['a.message']}\")\n\nexcept Exception as e:\n    print(f\"Error connecting to Neo4j or executing query: {e}\")\nfinally:\n    if driver:\n        driver.close()","lang":"python","description":"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."},"warnings":[{"fix":"Pass a `Config` object to the driver: `GraphDatabase.driver(uri, config=Config(encrypted=True, trust_strategy=TRUST_ALL_CERTIFICATES))`.","message":"Direct `encrypted` and `trusted_certificates` arguments for `GraphDatabase.driver` were removed. Connection encryption and trust settings must now be configured via a `neo4j.Config` object.","severity":"breaking","affected_versions":"5.0.0 and later"},{"fix":"Update transaction methods: `session.execute_read(my_read_function)` and `session.execute_write(my_write_function)`.","message":"The `session.read_transaction` and `session.write_transaction` methods have been deprecated in favor of `session.execute_read` and `session.execute_write`.","severity":"breaking","affected_versions":"5.0.0 and later"},{"fix":"Instead of `auth=AuthToken('basic', 'user', 'pass')`, use `from neo4j import basic_auth; auth=basic_auth('user', 'pass')`.","message":"The `AuthToken` class was moved from `neo4j` to `neo4j.auth`. It is now recommended to use helper functions like `basic_auth` directly.","severity":"breaking","affected_versions":"6.0.0 and later"},{"fix":"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`).","message":"Failing to explicitly call `driver.close()` on the `GraphDatabase.driver` instance can lead to resource leaks and open connections.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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`.","message":"Using incorrect URI schemes or ports, such as `http://` or `https://` (which are for HTTP API) instead of `bolt://` or `neo4j://` (for Bolt protocol).","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"search_vec":"'6.1.0':42 'alongsid':58 'api':31 'applic':21 'client':11 'cypher':34 'databas':18,66 'driver':3,7,69 'everi':55 'execut':33 'frequent':49 'graph':17,67 'handl':39 'high':27 'high-perform':26 'idiomat':30 'interact':14 'latest':45 'librari':12 'long':59 'lts':62 'major':52 'manag':36 'month':57 'neo4j':1,5,16,68 'offici':10 'perform':28 'provid':23 'python':2,6,20 'queri':35 'releas':47,54 'robust':25 'session':37 'stabil':65 'stabl':46 'support':61 'term':60 'transact':40 'updat':50 'version':41,53,63","created_at":"2026-04-09T03:54:14.459427+00:00","updated_at":"2026-04-17T01:21:43.233556+00:00","problems":[{"fix":"Double-check the credentials (username and password) passed to `basic_auth` or the `auth` parameter of `GraphDatabase.driver`.","cause":"Incorrect username or password provided during driver initialization.","error":"neo4j.exceptions.AuthError: The client is unauthorized to establish a session with the database."},{"fix":"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).","cause":"The Neo4j database server is not running, is inaccessible (e.g., due to a firewall), or the connection URI (host/port/protocol) is incorrect.","error":"neo4j.exceptions.ServiceUnavailable: Failed to establish a connection to the server."},{"fix":"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.","cause":"The `driver` or `session` object was not successfully initialized or was already closed before being used. This often occurs if an exception prevents `driver` assignment or if `driver.close()` is called prematurely.","error":"AttributeError: 'NoneType' object has no attribute 'run' (or 'close', 'session', etc.)"},{"fix":"Update your import statement to `from neo4j import GraphDatabase`.","cause":"Attempting to import the `GraphDatabase` class from an old, deprecated module (`neo4j.v1`).","error":"ImportError: cannot import name 'GraphDatabase' from 'neo4j.v1'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"6.2.0","cli_name":"","cli_version":null,"type":"library","homepage":"https://neo4j.com/","github":"https://github.com/neo4j/neo4j-python-driver","docs":null,"changelog":"https://github.com/neo4j/neo4j-python-driver/wiki","pypi":"https://pypi.org/project/neo4j/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-27","next_check":"2026-07-28","install_tag":null}}