py2neo:无法创建图形
py2neo: cannot create graph
我想尝试使用 py2neo,但甚至无法使用文档中的代码示例。例如,参见 here。代码是:
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)
其中returns几个错误信息:
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'
如果我理解的很好,API已经更改并且文档没有更新。但我使用的是版本 4,手册似乎是为该版本编写的。任何可以帮助我入门的指示?
好吧,我的错,我使用 py2neo 的方式不对。这是一段对我有用的代码:
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))
显然,Neo4j 服务器必须启动并且 运行 才能执行此代码。
我想尝试使用 py2neo,但甚至无法使用文档中的代码示例。例如,参见 here。代码是:
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)
其中returns几个错误信息:
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'
如果我理解的很好,API已经更改并且文档没有更新。但我使用的是版本 4,手册似乎是为该版本编写的。任何可以帮助我入门的指示?
好吧,我的错,我使用 py2neo 的方式不对。这是一段对我有用的代码:
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))
显然,Neo4j 服务器必须启动并且 运行 才能执行此代码。