Note: At the time of this writing, latest versions are Python 3.7.2 and Neo4j 3.5.3.
Neo4j is used to create and maintain Graph Databases, which are queryable using the Cypher language.
Installation of the virtual environment & packages
I assume you already have Python. If you don’t, download the latest version and follow the installation steps.
To manage virtual environments I use
pipenv. I’s a Python package that includes the features of both
virtualenv. It also uses ideas from
pew (another Python virtual environments manager) and
npm (official package manager of Node.s), so you can write
pipenv commands the same way you would do it with
pip and also use
pew magic to manage your projects.
Pipenv uses pip, so we need to make sure our
pip version is up to date. Don’t forget to also check your version of Python by typing
python -V and
pip -V before executing any command.
pip command itself is not recognized, you have a PATH problem to fix to access your librairies. Alternatively, you can use
python -m pip instead of
pip in the instructions below:
pip install --upgrade --user pip pipenv
pipenv install py2neo
The py2neo package depend on neo4j’s, so it will install it if you did not already. If this is the first time you use pipenv in your current directory, it will generate two files:
Pipfile: it stores the package’s names and potential constraints (version number, git link, …) given in the previous
pipenv install [packages]commands.
Pipfile.lock: it stores the hash of the versioned packages, it is updated automatically at every new installation and can be done manually using
pipenv update. The command
pipenv installwithout arguments will install packages as they are described in the Pipefile.lock file.
To enter in (“activate”) your virtual environment, just use
pipenv shell. It will load everything you need: the right Python’s version, the packages, environment variables, …
If you just want to activate the environment for one command, you can use
pipenv run [cmd], like
pipenv run python main.py.
Connect to Neo4j database
Once the installation finished, you should have your
$NEO4J_HOME environment variable set. You can then start running your Neo4j server using the command
restart works too):
sudo $NEO4J_HOME/bin/neo4j start
Now that we have both Neo4j and py2neo, we can use the latter to connect to our Neo4j databa’se.
import os from py2neo import Graph class GraphDriver(object): @classmethod def from_env(cls): uri: str = os.environ["NEO4J_URI"] usr: str = os.environ["NEO4J_USR"] pwd: str = os.environ["NEO4J_PASSWORD"] return cls(uri, usr, pwd) def __init__(self: object, uri: str, usr: str, pwd: str) -> None: self.graph: Graph = Graph(uri, auth=(usr, pwd))
This code allow to connect to a graph database using either explicit values or environment variables. Those who followed until then are thinking: convenient, since Pipenv loaded them for us!
Typically, those variables are stored in a
.env file (say “dot env”) at the root of the project’s folder or repository. So you only have to create this file and write key=value pairs, like for this example:
Those key-value pairs will be accessible next time you launch
pipenv shell. You can list them from your terminal by typing the command
printenv, or from your Python code using
import os; print(os.environ).
Perhaps you’ve noticed the link starts with bolt instead of usual http(s). Indeed, Neo4j engineers created this binary protocol to overcome HTTP(S) limitations in speed, payload and overall performances.
As you can see, the Bolt requests and responses are significantly smaller than HTTP’s. Also, the stateful set-up including in the Bolt world allow to not send multiple times all the informations, including authentification.
Now that you have everything set up, it’s time to populate your graph with tons of nodes and relationships! I personally suggest reading through the py2neo documentation to discover the different available objects (Nodes, Relationships, but also Graphs, Subgraphs, Record, Cursor, …) and how they interact.
In addition, you can read the py2neo API overview of Nigel Small, which was (and probably still is) the leader of the team that deployed py2neo.