I wrote a simple Python script to connect to my Neo4j database instance and retrieve some data using the official Neo4j library. I am trying to run this script on PythonAnywhere, but it failed to connect to the database, printing “Unable to retrieve routing information.” The URI, username, and password are correct; it successfully connects to the database from my desktop application written in Java.
Here is the full script in Python:
from neo4j import GraphDatabase
URI = "neo4j+s://uri:7687"
AUTH = ("usn", "pwd")
class PlayersNeo4jDatabaseManager:
def __init__(self):
try:
self.driver = GraphDatabase.driver(URI, auth=AUTH)
except Exception as ex:
print(ex)
def close(self):
self.driver.close()
def get_players_query(self, tx):
query = "MATCH (p:Player) RETURN p LIMIT 1;"
players = tx.run(query)
for player in players:
print(player)
def get_players(self):
with self.driver.session(database="neo4j") as session:
session.read_transaction(self.get_players_query)
playersNeo4jDatabaseManager = PlayersNeo4jDatabaseManager()
playersNeo4jDatabaseManager.get_players()
I tried neo4j://, neo4j+ssc, and bolt, but it did not work.
3
The connection to a Neo4j database on PythonAnywhere may fail due to network restrictions or firewall settings blocking the connection. PythonAnywhere typically doesn’t allow outbound connections to external services like databases. To resolve this, ensure the Neo4j instance is accessible over a public IP and configure PythonAnywhere’s outbound connection settings accordingly.
James Smith is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1