2

I was trying to connect neo4j by py2neo. But got some errors saying hostname or nodename not found...... I tried the below codes to connect but neither was working

graph = Graph()

#graph = Graph("http://username:password@localhost:7474/db/data/")

However, I could use browser to connect to the URL with the credential.

The below is the error message I got. Can someone help me with this?

File "test.py", line 56, in main
    graph = Graph()
  File "/anaconda2/lib/python2.7/site-packages/py2neo/database/__init__.py", line 327, in __new__
    use_bolt = version_tuple(inst.__remote__.get().content["neo4j_version"]) >= (3,)
  File "/anaconda2/lib/python2.7/site-packages/py2neo/database/http.py", line 154, in get
    response = self.__base.get(headers=headers, redirect_limit=redirect_limit, **kwargs)
  File "/anaconda2/lib/python2.7/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 "/anaconda2/lib/python2.7/site-packages/py2neo/packages/httpstream/http.py", line 943, in __get_or_head
    return rq.submit(redirect_limit=redirect_limit, **kwargs)
  File "/anaconda2/lib/python2.7/site-packages/py2neo/packages/httpstream/http.py", line 433, in submit
    http, rs = submit(self.method, uri, self.body, self.headers)
  File "/anaconda2/lib/python2.7/site-packages/py2neo/packages/httpstream/http.py", line 342, in submit
    raise NetworkAddressError(err.args[1], host_port=uri.host_port)
py2neo.packages.httpstream.http.NetworkAddressError: nodename nor servname provided, or not known: localhost:7474
mlo0424
  • 359
  • 2
  • 4
  • 16

2 Answers2

1

Make sure you have installed the pip, Neo4j is running on your machine and you have given correct login credentials.

This is how I use Py2neo in my Jupyter Notebook.

#Installing Py2Neo: Takes more than 30 seconds
!pip install py2neo

from py2neo import Graph, Node, Relationship
# Connecting Neo4j: Have to make sure Neo4j Community Edition or Ne4j Desktop is running
# It can be passed like this as well: graph = Graph(user="neo4j" password="yourpassword")
graph = Graph()
Mrinal Roy
  • 969
  • 7
  • 12
  • Yeah. I have installed pip and Neo4j on my mac. I could use browser to login to http://localhost:7474/db/data/ . However, I still could not connect to Neo4j by py2neo.... – mlo0424 Jan 10 '18 at 17:22
  • It can be passed like this as well: graph = Graph(user="neo4j", password="yourpassword") , you forgot the comma and edit queue is full – Tms91 Dec 09 '20 at 23:39
1

Problem solved. I found that there is no localhost defined at my /etc/hosts..... I just joined a new company and got a new mac. Not sure why it didn't put 127.0.0.1 localhost by default.....

mlo0424
  • 359
  • 2
  • 4
  • 16