首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >py2neo:无法创建图形

py2neo:无法创建图形
EN

Stack Overflow用户
提问于 2018-08-11 13:58:23
回答 1查看 2.1K关注 0票数 1

我想尝试一下py2neo,但甚至不能使用文档中的代码示例。请参见here示例。代码是:

代码语言:javascript
复制
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)

这将返回一些错误消息:

代码语言:javascript
复制
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,而手册似乎是为该版本编写的。有什么可以帮助我入门的建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-21 00:56:26

好吧,是我的错,我用错了py2neo。下面是一段适用于我的代码:

代码语言:javascript
复制
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服务器必须已启动并运行。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51796919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档