Jupyter中出现"_ipython_canary_method_should_not_exist_"的原因是什么?
解决Jupyter中连接JanusGraph出现"ipython_canary_method_should_not_exist"的问题
错误原因
这个报错是IPython(Jupyter的核心运行环境)的内部属性探查机制,和gremlin-python的延迟遍历对象冲突导致的:
- Gremlin的遍历语句是延迟执行的,
g.V().has('name','hercules').valueMap()只是定义了遍历逻辑,并没有实际发起请求执行; - Jupyter会自动探查变量对象的属性,用于实现代码补全、结果预览等功能,当它尝试访问Traversal对象中不存在的
_ipython_canary_method_should_not_exist_方法时,就会抛出这个异常。
另外你代码里还有个明显错误:Gremlin远程连接的协议应该用ws://(未加密)或wss://(加密),而非gs://,这个错误会导致连接失败,也可能间接触发IPython的对象探查报错。
解决办法
1. 触发遍历执行(推荐)
给遍历语句加上执行方法,让它立即发起请求并返回结果,避免IPython探查未执行的Traversal对象:
from gremlin_python import statics from gremlin_python.structure.graph import Graph from gremlin_python.process.graph_traversal import __ from gremlin_python.process.strategies import * from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection from gremlin_python.process.anonymous_traversal import traversal # 修正连接协议为ws:// g = traversal().withRemote(DriverRemoteConnection('ws://private_network:8182/janusgraph','g')) # 调用toList()执行遍历并获取结果列表 count = g.V().has('name','hercules').valueMap().toList() print(count)
常用的执行方法还有next()(获取单个结果)、toSet()(获取结果集合),根据需求选择即可。
2. 调整Jupyter的属性探查设置
如果不想修改代码,可以通过Jupyter魔法命令减少IPython对对象的自动探查:
%config IPython.core.interactiveshell.InteractiveShell.ast_node_interactivity = 'last_expr_or_assign'
这个设置会改变Jupyter的结果显示逻辑,减少对未执行对象的属性检查,但属于全局设置,可能影响其他代码的交互体验。
内容的提问来源于stack exchange,提问作者Archie
相关产品推荐
相关产品推荐

