1. 安装Python Neo4j模块:C:\windows\system32>pip install neo4j
2. 按装Python虚拟环境:pip install virtualenv
3. 注意运行目录(系统要允许在该目录下创建):C:\Users\bing.yao>python -m venv sandbox
4. 在虚拟环境Sandbox所在目录运行:C:\Users\bing.yao\sandbox\Scripts\activate.bat
5. 激活/进入虚拟环境后可查看Python版本,安装:pip install neo4j
6. 按照示例为现有节点创建关系,注意是现有节点
7. 按照示例查找节点
参考:https://docs.python.org/3/tutorial/venv.html#managing-packages-with-pip
参考:https://neo4j.com/docs/api/python-driver/4.2/
creatingNodes.py
from neo4j import GraphDatabase
uri = "neo4j://localhost:7687"
driver = GraphDatabase.driver(uri, auth=("neo4j", "xxxx"))
def create_friend_of(tx, name, friend):
tx.run("MATCH (a:Person) WHERE a.name = $name "
"CREATE (a)-[:KNOWS]->(:Person {name: $friend})",
name=name, friend=friend)
with driver.session() as session:
session.write_transaction(create_friend_of, "Rain", "Bill Pullman")
with driver.session() as session:
session.write_transaction(create_friend_of, "Rain", "Danny DeVito")
print("test1")
driver.close()
findingNodes.py
from neo4j import GraphDatabase
uri = "neo4j://localhost:7687"
driver = GraphDatabase.driver(uri, auth=("neo4j", "xxxx"))
def get_friends_of(tx, name):
friends = []
result = tx.run("MATCH (a:Person)-[:KNOWS]->(f) "
"WHERE a.name = $name "
"RETURN f.name AS friend", name=name)
for record in result:
friends.append(record["friend"])
return friends
with driver.session() as session:
friends = session.read_transaction(get_friends_of, "Rain")
for friend in friends:
print(friend)
driver.close()