python连接neo4j数据库


导入py2neo

由于Neo4j生态的向下兼容做的属实不大行,建议安装旧版本

pip install py2neo==4.3.0 -i https://pypi.douban.com/simple

若想安装新版本,由于指令格式不同,建议参考官方文档:

The Py2neo Handbook 2021.1

新旧版本Py2neo的Connection profiles指令对比

//旧版Connection profiles示例
from py2neo import *
graph = Graph('http://localhost:7474/',username='neo4j',password='123456')
 
//py2neo 2021.1版本的Connection profiles示例
from py2neo import *
graph = Graph("http://localhost:7474", auth=("neo4j", "123456"))
 
//详见py2neo 2021.1版本官方手册:https://py2neo.org/2021.1/profiles.html

创建两个节点

from py2neo import Graph, Node, Relationship
graph = Graph('http://localhost:7474', auth=("neo4j","drmzju2022"))
a=Node(label='person',name='lilian')
b=Node('person',name='jack')
r=Relationship(a,'love',b)
print(a,b,r)

运行截图:

 连接neo4j数据库

from py2neo import Graph,Node, Relationship
 
graph=Graph('http://localhost:7474',username='neo4j',password='neo4j')
 
a=Node('food',name='tree')
b=Node('food',name='flower')
graph.create(a)
graph.create(b)
 
r=Relationship(a,'beautiful',b)
graph.create(r)
 
print("graph")
print(graph)
print("node")
print(a,b)
print("relation")
print(r)

运行截图: