py2neo (Legacy)

2021.2.3 · abandoned · verified Thu Apr 16

Py2neo is a Python client library and toolkit for working with Neo4j graph databases. It provides a high-level API, an Object-Graph Mapper (OGM), and various utilities for interacting with Neo4j via both Bolt and HTTP protocols. As of 2023, the original `py2neo` project, and consequently `py2neo-history` (which mirrors `py2neo` up to version 2021.2.3), are considered End-of-Life (EOL) and are no longer maintained. Users are strongly recommended to migrate to the official Neo4j Python Driver or the `neomodel` OGM for ongoing development. This entry specifically refers to the `py2neo-history` package for accessing older versions.

Common errors

Warnings

Install

Imports

Quickstart

Connects to a Neo4j database using environment variables for credentials, creates a simple graph, and runs a Cypher query to retrieve data. This example demonstrates basic connection, node/relationship creation, and query execution.

import os
from py2neo import Graph, Node, Relationship

# Ensure Neo4j is running and accessible
uri = os.environ.get("NEO4J_URI", "bolt://localhost:7687")
username = os.environ.get("NEO4J_USERNAME", "neo4j")
password = os.environ.get("NEO4J_PASSWORD", "password")

try:
    graph = Graph(uri, auth=(username, password))
    # Verify connection and run a simple query
    result = graph.run("CREATE (a:Person {name: 'Alice'}) RETURN a.name AS name").data()
    print(f"Connected to Neo4j. Query result: {result}")

    # Create nodes and a relationship
    alice = Node("Person", name="Alice")
    bob = Node("Person", name="Bob")
    knows = Relationship(alice, "KNOWS", bob)
    graph.create(knows)
    print("Created Alice, Bob, and a KNOWS relationship.")

    # Fetch all Person nodes
    people = graph.run("MATCH (p:Person) RETURN p.name").data()
    print("People in the database:")
    for p in people:
        print(f"- {p['p.name']}")

except Exception as e:
    print(f"Error connecting to or querying Neo4j: {e}")

view raw JSON →