Native Python Driver VS. Py2neo
So you’ve decided to use Python to access your Neo4j database. Python is a great language and the Neo4j company has made it very easy to use Python to access Neo4j. Before you can access Neo4j with Python you need to select a Python driver to act as the interface from your Python application to the underlying Neo4j database.
Today there are three choices for connecting to and accessing Neo4j from Python.
- The Python driver supplied by Neo4j.
- The popular Py2Neo driver available on GitHub.
- neomodel – An OGM that sits on top of the native python driver supplied by Neo4j
Neo4j Python Driver
The Neo4j Python driver is an official part of the Neo4j product set so it is fully supported. That being said, it might not be the first driver to get updated when a new release of Neo4j comes out. There was a significant gap between the 4.x release of Neo4j and the Python driver. I expect this was due to the major architectural changes in V4 and probably won’t be as much of a problem going forward.
Neo4j Python Driver Examples
You can read the API details here. There is precious little in the way of example code and I hope to alleviate that with additional posts in this blog. Check out the related posts for examples of how to access Neo4j using Python.
Upgrading to V4 Python Driver
It’s not surprising that upgrading to the V4 Python driver will require some changes in your code. Neo4j V4 introduces the concept of multiple databases per running instance (or server) which was a much needed improvement. When connecting to a Neo4j server you will connect to the “default” database which is probably the one you want, but if you have a server with multiple application databases you will need to specify which one to use. Also, there are some procedures that only work against the “system” database which will fail if you don’t specify connecting to the system database.
A detailed explanation of issues related to migrating to the V4 driver can be3 found here including class name changes, import changes, URI scheme changes, commit logic changes and more.
Py2neo is the most popular choice for accessing Neo4j using Python. Py2neo is an open source project developed by Nigel Small who is well suited for this task as he works for Neo4j and is the Neo4j API expert. You can read the full API here or get the download from GitHub here. Historically, Py2Neo was the only choice in the early days of Neo4j as they did not supply a python driver.
Should I Use Py2neo Or The Official Neo4j Python Driver?
If you are considering using Python to access Neo4j the first question you will want to address is which python driver should I use to access Neo4j. Both choices work essentially the same way from a Python perspective, you do the necessary import statement to bring in the associated driver classes and use them to build a connection to Neo4j and run queries.
Before I give you my opinion let’s consult with the expert – Nigel himself…
When considering whether to use py2neo or the official Python Driver for Neo4j, there is a trade-off to be made. Py2neo offers a larger surface, with both a higher level API and an OGM, but the official driver provides mechanisms to work with clusters, such as automatic retries. If you are new to Neo4j, need an OGM, do not want to learn Cypher immediately, or require data science integrations, py2neo may be the better choice. If you are building a high-availability Enterprise application, or are using a cluster, you likely need the official driver.From the Py2neo Handbook
So this is a good explanation that matches pretty well to my experience developing a complex Neo4j application using Python – NodeEra. The initial version of NodeEra was developed using Py2neo but over time I realized I wasn’t using the OGM features and really just used Py2neo to access the underlying transaction and cypher execution features from the native driver. With V4 we are now using the native driver exclusively. Since the NodeEra application generates all of its own Cypher statements the OGM didn’t add any value.
Your application needs may be different from mine. Py2neo is a great choice for many applications including:
- Business applications can benefit from the OGM (Object Graph Mapping) features that allow you to model business data as classes and have the underlying graph database updated automatically (no cypher coding).
- Applications making use of other popular Python libraries such as Pandas will find convenience functions that make your life easier connecting to Neo4j data.
Neomodel is a relative new comer to the python neo4j driver scene. It has been developed specifically to run using the newer V4 native Python driver. It provides an object – graph mapping facility including integration with Django. I don’t have any experience with this so I can’t really comment any further but I think it is worth a look. Check it out here on PyPl.
So there you have it. There really is no “best” driver for accessing Neo4j using Python – only a range of your requirements which need to be matched to the two choices.
Comments are closed.