Cannot connect to Community Noe4j 2025 09 from Python on same machine

I have the latest Community version of Neo4j 2025 09 and cannot connect to it from a python script - on the same machine. Is there a problem with this version?

python code:


from neo4j import GraphDatabase

class SampleExample:

...

if _name_ == "_main_":

username = "neo4j"             
password = "..........."         

try:
    greeter = SampleExample(uri, username, password)
    greeter.print_greeting("Welcome to neo4j")
    greeter.close()
except Exception as e:
    print(f"Error: {e}")
    print("Please ensure Neo4j is running and the connection details are correct")

.....

Error Msg 1:

Failed to connect to Neo4j: Couldn't connect to 0.0.0.0:7687 (resolved to ('0.0.0.0:7687',)):
Failed to establish connection to ResolvedIPv4Address(('0.0.0.0', 7687)) (reason [Errno 111] Connection refused

Error Msg 2(occurs when I juggle the uri string (e.g., localhost:7687 or 0.0.0.0:7687, etc.):

Connection error: Couldn't connect to localhost:7687 (resolved to ('127.0.0.1:7687',)):
Failed to establish connection to ResolvedIPv4Address(('127.0.0.1', 7687)) (reason [Errno 111] Connection refused

Please help.  I downloaded neo4j specifically to connect with python.
Thanks

Curtis

Hi

it looks like you need to set the uri to neo4j://localhost:7687

Hi Jon,

I set uri to neo4j://localhost. Also, in the neo4j Desktop database, change the connection uri to same.

code:

uri = "neo4j://Localhost:7687"
username = "neo4j"
password = "…………."

try:
    greeter = SampleExample(uri, username, password)
    greeter.print_greeting("Welcome to neo4j")
    greeter.close()
except Exception as e:
    print(f"Error: {e}")
    print("Please ensure Neo4j is running and the connection details are correct")

error msg:

unable to retrieve routing information

Failed to connect to Neo4j: Unable to retrieve routing information
Error: Unable to retrieve routing information
Please ensure Neo4j is running and the connection details are correct

Note: As you can see, the last statement in error msg came from my try line.

My apologies. I read your query too quickly and gave you incorrect information

If you have a single neo4j servers running, then the URI would be

bolt://localhost:7687

Can you try that and let me know?

Again, apologies for the bad steer.

Jonathan

Jonathan,

No apologies necessary. Just glad you’re helping.

Made recommended changes. BTW, Neo4j is running with instance in Running state. Still getting errors:

Failed to connect to Neo4j: Couldn't connect to Localhost:7687 (resolved to ('127.0.0.1:7687',)):
Failed to establish connection to ResolvedIPv4Address(('127.0.0.1', 7687)) (reason [Errno 111] Connection refused)
Error: Couldn't connect to Localhost:7687 (resolved to ('127.0.0.1:7687',)):
Failed to establish connection to ResolvedIPv4Address(('127.0.0.1', 7687)) (reason [Errno 111] Connection refused)

Lets try my go to for connecting to Neo4j using Python

# Use the Neo4j python driver
from neo4j import GraphDatabase

# The cypher statement
cypher_statement = 'MATCH (n) RETURN n LIMIT 1'

# Detail to connect to our Neo4j database
neo4_uri = 'bolt://localhost:7687'
neo4j_user = 'neo4j'
neo4j_password = 'password'

# Connect to Neo4j
neo4jDB_connection = GraphDatabase.driver(neo4_uri, keep_alive=True, auth=(neo4j_user, neo4j_password))

# Send the Cypher statement to Neo4j
neo4j_response = neo4jDB_connection.execute_query(cypher_statement)

# Print the response
print(neo4j_response.records)

Give that a try and see how you get on.

Still errors:

Transaction failed and will be retried in 0.9537840946055642s (Couldn't connect to localhost:7687 (resolved to ('127.0.0.1:7687',)):
Failed to establish connection to ResolvedIPv4Address(('127.0.0.1', 7687)) (reason [Errno 111] Connection refused))
Transaction failed and will be retried in 1.9353899099803848s (Couldn't connect to localhost:7687 (resolved to ('127.0.0.1:7687',)):
Failed to establish connection to ResolvedIPv4Address(('127.0.0.1', 7687)) (reason [Errno 111] Connection refused))
Transaction failed and will be retried in 3.89372432112062s (Couldn't connect to localhost:7687 (resolved to ('127.0.0.1:7687',)):
Failed to establish connection to ResolvedIPv4Address(('127.0.0.1', 7687)) (reason [Errno 111] Connection refused))
Transaction failed and will be retried in 8.178613510251077s (Couldn't connect to localhost:7687 (resolved to ('127.0.0.1:7687',)):
Failed to establish connection to ResolvedIPv4Address(('127.0.0.1', 7687)) (reason [Errno 111] Connection refused))
Transaction failed and will be retried in 18.406962404285593s (Couldn't connect to localhost:7687 (resolved to ('127.0.0.1:7687',)):
Failed to establish connection to ResolvedIPv4Address(('127.0.0.1', 7687)) (reason [Errno 111] Connection refused))

Several lines following above error messages.

Here are half:

---------------------------------------------------------------------------
ConnectionRefusedError                    Traceback (most recent call last)
File ~/.local/lib/python3.11/site-packages/neo4j/_async_compat/network/_bolt_socket.py:498, in BoltSocketBase._connect_secure(cls, resolved_address, timeout, keep_alive, ssl_context)
    497 log.debug("[#0000]  C: <OPEN> %s", resolved_address)
--> 498 s.connect(resolved_address)
    499 s.settimeout(t)

ConnectionRefusedError: [Errno 111] Connection refused

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

ServiceUnavailable                        Traceback (most recent call last)
File ~/.local/lib/python3.11/site-packages/neo4j/_sync/io/_bolt_socket.py:329, in BoltSocket.connect(cls, address, tcp_timeout, deadline, custom_resolver, ssl_context, keep_alive)
    328 try:
--> 329     s = cls._connect_secure(
    330         resolved_address, tcp_timeout, keep_alive, ssl_context
    331     )
    332     agreed_version = s._handshake(resolved_address, deadline)

File ~/.local/lib/python3.11/site-packages/neo4j/_async_compat/network/_bolt_socket.py:521, in BoltSocketBase._connect_secure(cls, resolved_address, timeout, keep_alive, ssl_context)
    520 if isinstance(error, OSError):
--> 521     raise ServiceUnavailable(
    522         "Failed to establish connection to "
    523         f"{resolved_address!r} (reason {error})"
    524     ) from error
    525 raise

ServiceUnavailable: Failed to establish connection to ResolvedIPv4Address(('127.0.0.1', 7687)) (reason [Errno 111] Connection refused)

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

ServiceUnavailable                        Traceback (most recent call last)
Cell In[1], line 11
      9 neo4jDB_connection = GraphDatabase.driver(neo4_uri, keep_alive=True, auth=(neo4j_user, neo4j_password))
     10 # Send the Cypher statement to Neo4j
---> 11 neo4j_response = neo4jDB_connection.execute_query(cypher_statement)
     12 # Print the response
     13 print(neo4j_response.records)

File ~/.local/lib/python3.11/site-packages/neo4j/_sync/driver.py:946, in Driver.execute_query(self, query_, parameters_, routing_, database_, impersonated_user_, bookmark_manager_, auth_, result_transformer_, **kwargs)
    942     raise ValueError(
    943         f"Invalid routing control value: {routing_!r}"
    944     )
    945 with session._pipelined_begin:
--> 946     return session._run_transaction(
    947         access_mode,
    948         TelemetryAPI.DRIVER,
    949         work,
    950         (query_str, parameters, result_transformer_),
    951         {},
    952     )

File ~/.local/lib/python3.11/site-packages/neo4j/_sync/work/session.py:584, in Session._run_transaction(self, access_mode, api, transaction_function, args, kwargs)
    579         raise
    581 if errors:
    582     # TODO: 7.0 - when Python 3.11+ is the minimum,
    583     #             use exception groups
--> 584     raise errors[-1]
    585 else:
    586     raise ServiceUnavailable("Transaction failed")

Check that neo4j db is indeed running and that is listening to the port you expect. Test that it accepts connections on the port with netcat or other tool.

By the default, you should also have neo4j browser on http://localhost:7474 - try with that.

Then go to connecting with python. Check that you have the latest neo4j package for python.

By default the connection url for the driver should be neo4j://localhost:7687

Jonathan/Hakan,

Turns out, the problem was that my version of python was running inside of Jupyter running in Anaconda. The host was not my machine, but something in a cloud environment.

I downloaded and install a local version of Jupyter/python and then the recommendations you presented worked!

Thanks for you help.

Curtis

These days, it is getting harder to keep track of what is running where :slight_smile: Glad to hear you were able to sort it out.