将 py2neo 3.1.2 更新到 py2neo 4.0.0 时出现 ModuleNotFoundError

问题描述

在我们的应用程序中,我们通过 py2neo 模块版本 3.1.2 连接到 Neo4j 数据库。现在我们想将其更新到 4.0.0 版本。我们已经浏览了以下链接并了解需要进行哪些查询级别更改。

https://neo4j.com/docs/cypher-manual/current/deprecations-additions-removals-compatibility/#cypher-deprecations-additions-removals-4.0

但是,py2neo 4.0.0 版本中没有几个模块。请帮助我们了解是否有可用的等效模块。

from py2neo.database.status import ConstraintError
from py2neo.packages.neo4j.v1.exceptions import ProtocolError
from py2neo.packages.httpstream import http

以下是参考的错误详细信息。

ModuleNotFoundError: No module named 'py2neo.database.status'; 'py2neo.database' is not a package
Traceback (most recent call last):
  File "<stdin>",line 1,in <module>
ModuleNotFoundError: No module named 'py2neo.packages'

解决方法

py2neo 的新版本 4 不需要 http 而是使用 bolt。我用 py2neo 中的工作版本替换了所有丢失的模块。看我下面的例子;

#from py2neo.database.status import ConstraintError
from py2neo  import ClientError,GraphError
#from py2neo.packages.neo4j.v1.exceptions import ProtocolError
from py2neo  import DatabaseError,TransientError,TransactionError
#from py2neo.packages.httpstream import http  <-- http is replaced by bolt connection
from py2neo import Graph

print("py2neo version: ",py2neo.__version__)
graph = Graph("bolt://localhost:7687",auth=("neo4j","****"))
query = """<Invalid> RETURN $x as number,$x*$x as squared; """
try:
    cursor = graph.run(query,x=12)
    for record in cursor:
        print('The square of',record["number"],'is',record["squared"])
except (ClientError,GraphError) as ex:
    print('Client Graph error: \n',ex)
except (DatabaseError,TransactionError) as ex:
    print('DatabaseError error: \n',ex)
    
#references: https://py2neo.org/v4/index.html
#https://dzone.com/articles/introducing-bolt-neo4js-upcoming-binary-protocol-p

 RESULT:
 py2neo version:  4.2.0
 The square of 12 is 144