Showing results for 
Search instead for 
Did you mean: 

Python script to Neo4j - NameError: name 'session' is not defined

Node Clone


I'm getting the following error when trying to run a python script that is connected to my graph:

Traceback (most recent call last):
  File "/Users/Python scripts/python/", line 46, in <module>
    result =
NameError: name 'session' is not defined

 Here is my code snippet where the error occurs:

query = "MATCH (d:Document) WHERE size((a)-[:HAS_WORD]->(:Word)) = 0 RETURN as company, a.doctype as doctype, a.text as text"
result =

baselist = []
for record in result:   
for rec in baselist:
	company = rec["company"]
	ndocument = rec["doctype"]
	ntext = rec["text"]
	print("processing company %s doctype %s" % (company, ndoctype))
	parameters = { "companyv": company, "wordsv": getWords(ndoctype, stopwords, ps) };

Anyone know why this happens? I ran a very similar script a few minutes ago and it worked - but I'm stumped on this.

Any help very much appreciated



Share your full script. You're probably not creating the session in the right place.



Node Clone

Hi @michael_hunger - thank you - I actually realised the error - and it was, as you mentioned not creating the session correctly. Case closed !

Nodes 2022
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.