python - 使用 py2neo 将 python 连接到 neo4j 时出错

标签 python neo4j py2neo

我使用 py2neo 将以下 python 代码写入 neo4j

 from py2neo import Graph
 from py2neo import neo4j,Node,Relationship
 sgraph = Graph()
 alice = Node("person",name="alice")
 bob   = Node("person",name="bob")
 alice_knows_bob = Relationship(alice,"KNOWS",bob)
 sgraph.create(alice_knows_bob)

但是我得到了以下错误

Traceback (most recent call last):
File "C:\Python34\lib\site-packages\py2neo\core.py", line 258, in get
response = self.__base.get(headers=headers, redirect_limit=redirect_limit, *
*kwargs)
File "C:\Python34\lib\site-packages\py2neo\packages\httpstream\http.py",line
966, in get
return self.__get_or_head("GET", if_modified_since, headers, redirect_limit,
**kwargs)
File "C:\Python34\lib\site-packages\py2neo\packages\httpstream\http.py",line
943, in __get_or_head
return rq.submit(redirect_limit=redirect_limit, **kwargs)
File "C:\Python34\lib\site-packages\py2neo\packages\httpstream\http.py",line
452, in submit
return Response.wrap(http, uri, self, rs, **response_kwargs)
File "C:\Python34\lib\site-packages\py2neo\packages\httpstream\http.py",line
489, in wrap
raise inst
py2neo.packages.httpstream.http.ClientError: 401 Unauthorized

During handling of the above exception, another exception occurr ed:

Traceback (most recent call last):
File "neo.py", line 7, in <module>
sgraph.create(alice_knows_bob)
File "C:\Python34\lib\site-packages\py2neo\core.py", line 704, in create
statement = CreateStatement(self)
File "C:\Python34\lib\site-packages\py2neo\cypher\create.py", 44,in__init__
self.supports_node_labels = self.graph.supports_node_labels
File "C:\Python34\lib\site-packages\py2neo\core.py", line 1078, in   supports_node_labels return self.neo4j_version >= (2, 0)
File "C:\Python34\lib\site-packages\py2neo\core.py", line 956, in  neo4j_version
return version_tuple(self.resource.metadata["neo4j_version"])
File "C:\Python34\lib\site-packages\py2neo\core.py", line 213, in metadata
self.get()
File "C:\Python34\lib\site-packages\py2neo\core.py", line 261, in get
raise Unauthorized(self.uri.string)
py2neo.error.Unauthorized: http://localhost:7474/db/data/

谁能帮帮我。这是我第一次编写 python 代码来连接到 neo4j。

最佳答案

如果您使用的是 Neo4j 2.2,则默认情况下会启用数据库服务器的身份验证。在执行进一步操作之前,您需要进行身份验证。阅读documentation .

from py2neo import authenticate, Graph

# set up authentication parameters
authenticate("localhost:7474", "user", "pass")

# connect to authenticated graph database
sgraph = Graph("http://localhost:7474/db/data/")

# create alice and bob
...

来自同一个documentation ,

Py2neo provides a command line tool to help with changing user passwords as well as checking whether a password change is required.

对于新安装,使用:

$ neoauth neo4j neo4j my-p4ssword
Password change succeeded

After a password has been set, the tool can also be used to validate credentials

$ neoauth neo4j my-p4ssword
Password change not required

关于python - 使用 py2neo 将 python 连接到 neo4j 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29986317/

相关文章:

python - 如何运行文本文件中包含的可执行文件(exe 的十六进制)

ruby-on-rails - Neo4j多个neo4j实例

neo4j - 如何使用 py2neo 创建唯一节点

join - Neo4j - 查询两个不同的图(相当于对来自不同数据库的表进行 SQl 连接)

python - 在 BOLT neo4j-driver 中使用 python datetime 类型会引发错误。可用的解决方法?

http - 从远程服务器提交 neo4j 批处理时出现 IncompleteRead 错误;格式错误的 HTTP 响应

python - pandas 中 resample 和 asfreq 的不同行为

python - 用 Rust 编写的 PyPy + Cpython 扩展 (rust-cpython)

python - 多处理 : optimize CPU usage for concurrent HTTP async requests

java - Neo4j 和 ORM( hibernate )