Running a Cypher Query With Parameters Through a Python Script

Madison Gipson
Dec 13, 2020 · 2 min read
Clean, minimal workspace setup with laptop displaying code in front of a window.

I’ve been working on a project for a few months now that has involved tying a Neo4j database to a Dash application, which ultimately comes down to being able to run Cypher queries through Python code.

The library Py2Neo has been incredibly useful for this; you can run Cypher queries as-is or use the Py2Neo functions to perform similar actions.
My personal preference is running Cypher queries as-is because I can simply copy and paste the code to and from the Neo4j browser.

Cypher Query in Neo4j Browser: 
MATCH (n:Person) RETURN n.name AS name
Cypher Query with Py2Neo:
graph.run("""MATCH (n:Person) RETURN n.name AS name""").data()

See how easy that is?

This article details Py2Neo further.

Sometimes we need to dynamically substitute parameters into a query though. For a class project earlier this semester, I was trying to add descriptions for people nodes in my database.

This involved iterating through the names of people nodes, grabbing the person’s description from the Wikipedia API, then assigning the description to the person node in question. Doing this requires substitution of the person’s name into a Cypher query; the following code is how this was done.

It’s a simple substitution on the string containing the Cypher query — nothing fancy, but incredibly useful and easy!

I hope this short tutorial was useful. If you have any additional methods to accomplish the same goal, please comment and let’s share our knowledge!

By the way, here’s the larger project from which this was taken!

If you’d like to see how I’ve leveraged Py2Neo in an automated script that builds a Neo4j database, check out this article!

The Startup

Get smarter at building your thing. Join The Startup’s +787K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +787K followers.

Madison Gipson

Written by

Student & intern learning how to make the world a better place with code.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +787K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store