# Simple example using the python driver?

**URL:** <https://community.neo4j.com/t/simple-example-using-the-python-driver/40897>\
**Category:** Python\
**Created:** [July 5, 2021, 7:57am UTC](https://community.neo4j.com/t/simple-example-using-the-python-driver/40897 "2021-07-05T07:57:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![D\_C](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/d_c/32/19041_2.png) [@D\_C](https://community.neo4j.com/u/D_C)\
**Post date:** [July 5, 2021, 7:57am UTC](https://community.neo4j.com/t/simple-example-using-the-python-driver/40897/1 "2021-07-05T07:57:52Z")

</div>

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](https://neo4j.com/docs/api/python-driver/current/#quick-example) 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 = tx.run("CREATE (a:Greeting) "`

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

I did just try a simpler:

```auto
def raw_query(query, **kwargs):
    neodriver = neo_connect() # cached dbconn
    with neodriver.session() as session:
        try:
            result = session.run(query, **kwargs)
            return result.data()

```

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

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

[dfcx/ __init__] ERROR | Failed to write data to connection IPv4Address(('neo4j-core-8afc8558-3.production-orch-0042.neo4j.io', 7687)) (IPv4Address(('34.82.120.138', 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:

```auto
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")
    else:
        return raw_driver

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

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

```

---

<div class="post-metadata">

**Author:** ![florent\_biville](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/florent_biville/32/14205_2.png) [@florent\_biville](https://community.neo4j.com/u/florent_biville)\
**Post date:** [July 5, 2021, 8:14am UTC](https://community.neo4j.com/t/simple-example-using-the-python-driver/40897/2 "2021-07-05T08:14:20Z")

</div>

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](https://py2neo.org/v4/ogm.html).  
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 using the neo4j-python-driver](https://github.com/neo4j-examples/movies-python-bolt/)

EDIT: please refrain from [duplicating posts](https://stackoverflow.com/questions/68252567/simple-example-of-working-with-neo4j-python-driver) across different channels. We're monitoring all of them and this needlessly fragments our effort to answer questions.

---

<div class="post-metadata">

**Author:** ![D\_C](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/d_c/32/19041_2.png) [@D\_C](https://community.neo4j.com/u/D_C)\
**Post date:** [July 5, 2021, 8:49pm UTC](https://community.neo4j.com/t/simple-example-using-the-python-driver/40897/3 "2021-07-05T20:49:17Z")

</div>

as mentioned in the post above, I've tried py2neo, but [hit a roadblock](https://community.neo4j.com/t/p2neo-on-match/40879) 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?

---

<div class="post-metadata">

**Author:** ![florent\_biville](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/florent_biville/32/14205_2.png) [@florent\_biville](https://community.neo4j.com/u/florent_biville)\
**Post date:** [July 6, 2021, 8:34am UTC](https://community.neo4j.com/t/simple-example-using-the-python-driver/40897/4 "2021-07-06T08:34:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![D\_C](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/d_c/32/19041_2.png) [@D\_C](https://community.neo4j.com/u/D_C)\
**Post date:** [July 6, 2021, 10:56am UTC](https://community.neo4j.com/t/simple-example-using-the-python-driver/40897/5 "2021-07-06T10:56:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![michael.hunger](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/michael.hunger/32/27377_2.png) [@michael.hunger](https://community.neo4j.com/u/michael.hunger)\
**Post date:** [July 6, 2021, 8:04pm UTC](https://community.neo4j.com/t/simple-example-using-the-python-driver/40897/6 "2021-07-06T20:04:56Z")

</div>

We tried to provide a simpler example for the sandboxes:

> <https://github.com/neo4j-graph-examples/northwind/blob/main/code/python/example.py#L16-L21>

---

<div class="post-metadata">

**Author:** ![D\_C](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/d_c/32/19041_2.png) [@D\_C](https://community.neo4j.com/u/D_C)\
**Post date:** [July 7, 2021, 3:56am UTC](https://community.neo4j.com/t/simple-example-using-the-python-driver/40897/7 "2021-07-07T03:56:56Z")

</div>

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