PyCharm 2022.1.2 Jupyter开启autoreload后调试无法正常工作
PyCharm 2022.1.2 Jupyter开启autoreload后调试失效问题修复
问题复现条件
- 受影响版本:2022年6月前发布的PyCharm 2022.1.2(当时的最新正式版)
- 环境说明:PyCharm默认配置,使用官方完全支持的mini-conda环境,环境配置不是故障诱因
- 触发操作:在Jupyter单元格运行以下代码开启全量自动重载功能
%load_ext autoreload %autoreload 2
故障表现
开启上述功能后,Jupyter单元格内的Python调试功能完全失效,调试器无法正常挂载运行,仅输出如下报错栈:
Traceback (most recent call last): File "/home/myUserName/pycharm-2022.1/plugins/python/helpers/pydev/_pydevd_bundle/pydevd_trace_dispatch_regular.py", line 429, in __call__ abs_path_real_path_and_base = NORM_PATHS_AND_BASE_CONTAINER[frame.f_code.co_filename] KeyError: '/home/myUserName/pycharm-2022.1/plugins/python/helpers/pydev/pydevd_file_utils.py' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/myUserName/pycharm-2022.1/plugins/python/helpers/pydev/pydevd_file_utils.py", line 580, in get_abs_path_real_path_and_base_from_frame return NORM_PATHS_AND_BASE_CONTAINER[frame.f_code.co_filename] NameError: name 'NORM_PATHS_AND_BASE_CONTAINER' is not defined During handling of the above exception, another exception occurred:
关闭autoreload功能后调试器可恢复正常,但会丢失代码修改后自动重载的能力。该问题属于PyCharm 2022.1.2版本内置调试器pydev的兼容性bug,并非用户配置错误导致。
可用解决方案
按操作成本从低到高排序:
- 调整autoreload重载等级
无需修改任何程序文件,将全量重载模式改为指定模块重载模式即可规避bug:
该模式下仅会重载你通过%load_ext autoreload %autoreload 1%aimport 你的模块名手动标记的模块,不会触发全模块重载逻辑,不会触发调试器作用域报错,缺点是无法自动重载所有修改过的文件,需要手动指定重载目标。 - 手动打补丁修复调试器文件
打开报错路径中提到的pydevd_file_utils.py文件,路径格式为[你的PyCharm安装目录]/plugins/python/helpers/pydev/pydevd_file_utils.py,定位到第580行附近的get_abs_path_real_path_and_base_from_frame函数,在函数第一行添加全局变量声明:
保存文件后重启PyCharm即可,修复后可以正常使用global NORM_PATHS_AND_BASE_CONTAINER%autoreload 2全量重载+调试功能。该bug的根因是autoreload重载模块时,这个函数访问全局路径缓存变量前没有声明global,触发了跨作用域调用报错。 - 升级PyCharm版本
该兼容性bug在PyCharm 2022.2及后续正式版本中已经被官方修复,直接升级到更高版本即可无改造同时保留全量autoreload和Jupyter调试能力。
内容的提问来源于stack exchange,提问作者puf
相关产品推荐
相关产品推荐

