Python – Using Neo4j from Python- Best Guide
For upon |This guide provides an overview of how to connecting to Neo4j from Python. While it is not comprehensive, it aims to introduce the available drivers and links to other relevant resources.
- You should be familiar with graph database concepts and the property graph model.
- You should have installed Neo4j and made yourself familiar with our Cypher query language.
- We also recommend installing and becoming familiar with both pip and virtual env before working on a project.
Neo4j and Python
Neo4j can be installed on any system and then accessed via its binary and HTTP APIs.
You can use the official binary driver for Python (neo4j-python-driver) or connect via HTTP with any of our community drivers.
Neo4j Python Driver
The Neo4j Python driver is officially supported by Neo4j and connects to the database using the binary protocol. It aims to be minimal while being idiomatic to Python.
pip install neo4j
from neo4j import GraphDatabase class HelloWorldExample: def __init__(self, uri, user, password): self.driver = GraphDatabase.driver(uri, auth=(user, password)) def close(self): self.driver.close() def print_greeting(self, message): with self.driver.session() as session: greeting = session.write_transaction(self._create_and_return_greeting, message) print(greeting) def _create_and_return_greeting(tx, message): result = tx.run("CREATE (a:Greeting) " "SET a.message = $message " "RETURN a.message + ', from node ' + id(a)", message=message) return result.single()[0] if __name__ == "__main__": greeter = HelloWorldExample("bolt://localhost:7687", "neo4j", "password") greeter.print_greeting("hello, world") greeter.close()
Name |
Version |
Authors |
neo4j-driver |
4.0 |
The Neo4j Team, Nigel Small |
The Example Project
The Neo4j example project is a small, one-page web app for the movies database built into the Neo4j tutorial. The front-end page is the same for all drivers: movie search, movie details, and a graph visualization of actors and movies. Each backend implementation shows you how to connect to Neo4j from each of the different languages and drivers.
You can learn more about our small, consistent example project across many different language drivers here. You will find the implementations for all drivers as individual GitHub repositories, which you can clone and deploy directly.
Neo4j Community Drivers
Members of each programming language community have invested a lot of time and love to develop each one of the community drivers for Neo4j, so if you use any one of them, please provide feedback to the authors.
The community drivers have been graciously contributed by the Neo4j community. Many of them are fully featured and well-maintained, but some may not be. Neo4j does not take any responsibility for its usability. |
For anyone working with Python, the Neo4j community has contributed a range of driver options. These range from lightweight to comprehensive driver packages as well as libraries designed for use with web frameworks such as Django.
While we do not provide a specific web framework recommendation, both the lightweight Flask and the more comprehensive Django frameworks are known to work well.
Py2neo
Py2neo is a client library and comprehensive toolkit for working with Neo4j from within applications and from the command line. It has been carefully designed to be easy and intuitive to use.
Author |
|
Package |
https://pypi.python.org/pypi/py2neo |
Source |
https://github.com/nigelsmall/py2neo |
Example |
https://github.com/neo4j-examples/movies-python-py2neo |
Docs |
http://py2neo.org/ |
Python |
2.7 / 3.4+ |
Protocols |
Bolt, Http |
pip install py2neo
from py2neo import Graph graph = Graph() tx = graph.begin() for name in ["Alice", "Bob", "Carol"]: tx.append("CREATE (person:Person {name:{name}}) RETURN person", name=name) alice, bob, carol = [result.one for result in tx.commit()]
Neo model
An Object Graph Mapper built on top of the Neo4j Python driver. Familiar Django-style node definitions with a powerful query API, thread-safe, and full transaction support. A Django plugin django_neomodel is also available.
Author |
Athanasios Anastasiou and Robin Edwards |
Package |
https://pypi.python.org/pypi/neomodel |
Source |
http://github.com/neo4j-contrib/neomodel |
Docs |
http://neomodel.readthedocs.org/ |
Python |
2.7 / 3.3+ |
Protocols |
Bolt |
pip install neomodel
from neomodel import StructuredNode, StringProperty, RelationshipTo, RelationshipFrom, config config.DATABASE_URL = 'bolt://neo4j:test@localhost:7687' class Book(StructuredNode): title = StringProperty(unique_index=True) author = RelationshipTo('Author', 'AUTHOR') class Author(StructuredNode): name = StringProperty(unique_index=True) books = RelationshipFrom('Book', 'AUTHOR') harry_potter = Book(title='Harry potter and the..').save() rowling = Author(name='J. K. Rowling').save() harry_potter.author.connect(rowling)
Comments are closed.