简体   繁体   中英

neo4j: Failed to connect to DB [py2neo]

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

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()

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.....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM