You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Neo4j Python驱动查询时遇AttributeError问题求助

问题解决:AttributeError: 'Session' object has no attribute 'execute_read' 及查询结果转JSON

报错原因

execute_read 是Neo4j Python驱动4.4.x及以上版本才新增的方法,若你使用的是4.3.x及更早版本,Session对象不存在该属性,因此触发报错。

解决方案

方案1:升级Neo4j Python驱动

允许升级的话,执行以下命令更新驱动:

pip install --upgrade neo4j

升级后原代码可正常运行,tx.run(cypherQuery).data() 返回字典组成的列表,直接用json.dumps()即可转为JSON格式:

import json
from neo4j import GraphDatabase

driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "your_password"))
cypherQuery="MATCH (n:BusinessApplication) RETURN n"

with driver.session() as session:
    results = session.execute_read(lambda tx: tx.run(cypherQuery).data())  
# 转换为JSON格式
json_result = json.dumps(results, indent=2)
print(json_result)

driver.close()

方案2:适配旧版本驱动(不升级)

无法升级驱动时,直接用Session的run方法替代execute_read,调整后代码如下:

import json
from neo4j import GraphDatabase

driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "your_password"))
cypherQuery="MATCH (n:BusinessApplication) RETURN n"

with driver.session() as session:
    # 旧版本直接调用session.run()获取结果
    results = session.run(cypherQuery).data()  
# 转换为JSON格式
json_result = json.dumps(results, indent=2)
print(json_result)

driver.close()

补充说明

  • tx.run().data() 返回的结果是包含节点属性字典的列表,每个节点的属性会嵌套在n键下(示例:{"n": {"name": "DemoApp", "id": 1001}})
  • 若想让节点属性直接作为顶级键,可修改Cypher查询为 MATCH (n:BusinessApplication) RETURN properties(n) as app,转换后的JSON结构会更简洁

内容的提问来源于stack exchange,提问作者Tono Kuriakose

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 14:11:12