Skip to content

Error connecting python to neo4j using py2neo

I wrote the following python code to neo4j using py2neo

 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)

but i got the following error

Traceback (most recent call last):
File "", line 258, in get
response = self.__base.get(headers=headers, redirect_limit=redirect_limit, *
File "",line
966, in get
return self.__get_or_head("GET", if_modified_since, headers, redirect_limit,
File "",line
943, in __get_or_head
return rq.submit(redirect_limit=redirect_limit, **kwargs)
File "",line
452, in submit
return Response.wrap(http, uri, self, rs, **response_kwargs)
File "",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 "", line 7, in <module>
File "", line 704, in create
statement = CreateStatement(self)
File "", 44,in__init__
self.supports_node_labels = self.graph.supports_node_labels
File "", line 1078, in   supports_node_labels return self.neo4j_version >= (2, 0)
File "", line 956, in  neo4j_version
return version_tuple(self.resource.metadata["neo4j_version"])
File "", line 213, in metadata
File "", line 261, in get
raise Unauthorized(self.uri.string)
py2neo.error.Unauthorized: http://localhost:7474/db/data/

can anyone please help me.This is the first time i writing python code to connect to neo4j.


If you’re using Neo4j 2.2, authentication for database servers is enabled by default. You need to authenticate before performing further operations. Read 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

From the same documentation,

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

For a new installation, use:

$ 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