Py2neo is a simple and pragmatic Python library that provides access to the popular graph database Neo4j via its RESTful web service interface. With no external dependencies, installation is straightforward and getting started with coding is easy. The library is actively maintained on GitHub, regularly updated in the Python Package Index and is built uniquely for Neo4j in close association with its team and community.
In addition, the library provides support for the Graph Export Object File Format (Geoff). For further information on Geoff, visit http://geoff.nigelsmall.net/.
Py2neo has been built and tested against the following software:
- Python 2.6+ <http://python.org/>
- Neo4j 1.6+ <http://neo4j.org/>
Other versions may work but are not guaranteed to do so. If you are using Jython, IronPython or Python 3 then please help to improve support for these versions by providing feedback.
The easiest way to install py2neo is from the Python Package Index (PyPI), using pip:
sudo pip install py2neo
Alternatively, you may prefer to use the latest code directly from GitHub:
git clone git@github.com:nigelsmall/py2neo.git
The following short programme illustrates a simple usage of the py2neo library:
<<code lang="python">>
#!/usr/bin/env python """ Simple example showing node and relationship creation plus execution of Cypher queries """ from __future__ import print_function # Import Neo4j modules from py2neo import neo4j, cypher # Attach to the graph db instance graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data/") # Create two nodes node_a, node_b = graph_db.create( {"name": "Alice"}, {"name": "Bob"} ) # Join the nodes with a relationship rel_ab = node_a.create_relationship_to(node_b, "KNOWS") # Build a Cypher query query = "START a=node({A}) MATCH a-[:KNOWS]->b RETURN a,b" # Define a row handler... def print_row(row): a, b = row print(a["name"] + " knows " + b["name"]) # ...and execute the query cypher.execute(graph_db, query, {"A": node_a.id}, row_handler=print_row)
<</code>>
For more examples, browse the tutorials or for full details of the functionality available, check out the API documentation.