py2neo erroring for basic requests with neo4j

524 views Asked by At

I have spent the last few months running neo4j on my local machine. I have been using py2neo to orchestrate the db with python. I have just switched to running the database on an aws ec2 instance and I am recieving errors whenever I try to send requests to the database. I am using the following simple code to test the database, this code works perfectly on my local machine.

from py2neo import *

neo4j_url = 'http://localhost:7474/'
user = 'neo4j'
pwd = 'neo4j'

graph = Graph(neo4j_url, auth=(user,pwd))
print(graph)
graph.run("Create (n:Person{name: 'Andy person'})")

This code errors with

Graph('http://localhost:7474')
Traceback (most recent call last):
  File "/home/ec2-user/KG_construction/kg-construction/KG_env/lib64/python3.8/site-packages/py2neo/client/http.py", line 443, in from_json
    content = json_loads(data, object_hook=JSONHydrant.json_to_packstream)
  File "/usr/lib64/python3.8/json/__init__.py", line 370, in loads
    return cls(**kw).decode(s)
  File "/usr/lib64/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib64/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "test_connection.py", line 9, in <module>
    graph.run("Create (n:Person{name: 'Andy person'})")
  File "/home/ec2-user/KG_construction/kg-construction/KG_env/lib64/python3.8/site-packages/py2neo/database.py", line 405, in run
    return self.auto().run(cypher, parameters, **kwparameters)
  File "/home/ec2-user/KG_construction/kg-construction/KG_env/lib64/python3.8/site-packages/py2neo/database.py", line 989, in run
    result = self._connector.auto_run(cypher, parameters,
  File "/home/ec2-user/KG_construction/kg-construction/KG_env/lib64/python3.8/site-packages/py2neo/client/__init__.py", line 1340, in auto_run
    return cx.auto_run(cypher, parameters, graph_name=graph_name, readonly=readonly)
  File "/home/ec2-user/KG_construction/kg-construction/KG_env/lib64/python3.8/site-packages/py2neo/client/http.py", line 177, in auto_run
    rs = HTTPResponse.from_json(r.status, r.data.decode("utf-8"))
  File "/home/ec2-user/KG_construction/kg-construction/KG_env/lib64/python3.8/site-packages/py2neo/client/http.py", line 445, in from_json
    raise_from(ProtocolError("Cannot decode response content as JSON"), error)
  File "<string>", line 3, in raise_from
py2neo.errors.ProtocolError: Cannot decode response content as JSON

Does anyone have idea why this error is occuring on my ec2 instance but not on my local machine?

2

There are 2 answers

0
Yutong FEI On

I had the same problem as you and I solved it. The problem was in the source code of py2neo. In the py2neo/database.py file, at line 990, I changed the following code :

else:
            result = self._connector.auto_run(cypher, parameters,
                                              graph_name="neo4j",
                                              readonly=self.readonly)

Hope this will help you. :)

0
Yutong FEI On

Another solution is that when we connect to neo4j, we should write the following code :

graph  = Graph("http://localhost:____", auth = ("neo4j", "password"), name="neo4j")