I wanted to experiment with py2neo, but cannot even use the code samples from the documentation. See here for example. The code is:
from py2neo import Graph, Node, Relationship
g = Graph()
tx = g.begin()
a = Node("Person", name="Alice")
tx.create(a)
b = Node("Person", name="Bob")
ab = Relationship(a, "KNOWS", b)
tx.create(ab)
tx.commit()
g.exists(ab)
which returns a few error messages:
Traceback (most recent call last):
File ".\test_py2neo.py", line 22, in <module>
tx = g.begin()
File "C:\Users\saran\AppData\Local\Programs\Python\Python35\lib\site-packages\py2neo\database.py", line 335, in begin
return Transaction(self, autocommit)
File "C:\Users\saran\AppData\Local\Programs\Python\Python35\lib\site-packages\py2neo\database.py", line 797, in __init__
self.transaction = self.session.begin_transaction()
AttributeError: 'NoneType' object has no attribute 'begin_transaction'
Exception ignored in: <bound method Driver.__del__ of <neo4j.v1.api.Driver object at 0x000001325C9F0940>>
Traceback (most recent call last):
File "C:\Users\saran\AppData\Local\Programs\Python\Python35\lib\site-packages\neo4j\v1\api.py", line 151, in __del__
File "C:\Users\saran\AppData\Local\Programs\Python\Python35\lib\site-packages\neo4j\v1\api.py", line 193, in close
AttributeError: 'str' object has no attribute 'close'
If I understand well, the API has changed and the documentation has not been updated. But I am using version 4 and the manual seems to be written for that version. Any pointers that could help me get started?
Ok, my bad, I was using py2neo the wrong way. Here is a piece of code that works for me:
from py2neo import Graph, Node, Relationship
uri = "bolt://localhost:7687"
user = "neo4j"
password = "..."
g = Graph(uri=uri, user=user, password=password)
# optionally clear the graph
# g.delete_all()
print(len(g.nodes))
print(len(g.relationships))
# begin a transaction
tx = g.begin()
# define some nodes and relationships
a = Node("Person", name="Alice")
b = Node("Person", name="Bob")
ab = Relationship(a, "KNOWS", b)
# create the nodes and relationships
tx.create(a)
tx.create(b)
tx.create(ab)
# commit the transaction
tx.commit()
print(g.exists(ab))
print(len(g.nodes))
print(len(g.relationships))
Obviously, the Neo4j server must be up and running before executing this code.