Showing results for 
Search instead for 
Did you mean: 

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

Simple example using the python driver?

Node Clone

Is there a simple example of working with the neo4j python driver?
How do I just pass cypher query to the driver to run and return a cursor?

If I'm reading for example this it seems the demo has a class wrapper, with a private member func I pass to the session.write,

session.write_transaction(self._create_and_return_greeting, ...

That then gets called it with a transaction as a first parameter...

def _create_and_return_greeting(tx, message):

that in turn runs the cypher

result ="CREATE (a:Greeting) "

This seems 10X more complicated than it needs to be.

I did just try a simpler:

def raw_query(query, **kwargs):
    neodriver = neo_connect()  # cached dbconn
    with neodriver.session() as session:
            result =, **kwargs)

But this results in a socket error on the query, probably because the session goes out of scope?

[dfcx/__init__] ERROR | Underlying socket connection gone (_ssl.c:2396)

[dfcx/__init__] ERROR | Failed to write data to connection IPv4Address(('', 7687)) (IPv4Address(('', 7687)))

Also I can't return a cursor/iterator, just the data()
When the session goes out of scope, the query result seems to die with it.

If I manually open and close a session, then I'd have the same problems?

Python must be the most popular language this DB is used with, does everyone use a different driver?
Py2neo seems cute, but completely lacking in ORM wrapper function for most of the cypher language features, so you have to drop down to raw cypher anyway. And I'm not sure it supports **kwargs argument interpolation in the same way.

I guess that $300M raise got blown on marketing.

Slightly longer version trying to get a working DB wrapper:

def neo_connect() -> Union[neo4j.BoltDriver, neo4j.Neo4jDriver]:

    global raw_driver
    if raw_driver:
        # print('reuse driver')
        return raw_driver

    neoconfig = NEOCONFIG
    raw_driver = neo4j.GraphDatabase.driver(
        neoconfig['url'], auth=(
            neoconfig['user'], neoconfig['pass']))
    if raw_driver is None:
        raise BaseException("cannot connect to neo4j")
        return raw_driver

def raw_query(query, **kwargs):
    # just get data, no cursor
    neodriver = neo_connect()
    session = neodriver.session()
    #'neoquery %s', query)
    # with neodriver.session() as session:
        result =, **kwargs)
        data =
        return data

    except neo4j.exceptions.CypherSyntaxError as err:
        logging.error('neo error %s', err)
        logging.error('failed query: %s', query)
        raise err
    # finally:
    #'close session')
    #     session.close()

I'm not a Python user, but as far as I know, Py2Neo offers OGM support: 4. py2neo.ogm – Object-Graph Mapping — The Py2neo v4 Handbook.
For a simple example with the raw Python driver, you can find one here: GitHub - neo4j-examples/movies-python-bolt: Neo4j Movies Example application with Flask backend usin...

EDIT: please refrain from duplicating posts across different channels. We're monitoring all of them and this needlessly fragments our effort to answer questions.

Node Clone

as mentioned in the post above, I've tried py2neo, but hit a roadblock almost immediately, so it doesn't seem like a mature option.

re crossposting: I didn't get much response here on previous questions, so I'm trying to find an active community for this DB. StackOverflow seems quiet too. Is there a slack/discord with more activity?

Py2neo has been developed for 10+ years, so calling it immature is quite a harsh statement, to say the least.

To be fair, most of that was as an unofficial side project, and a great effort by the guy working on it.

We tried to provide a simpler example for the sandboxes:

Node Clone

this looks useful, I might try this.
I have scoured the docs over the weekend and never seen this...