VS Code调试器中Numpy数组无限展开及Jedi服务异常问题
问题描述
- 运行以下Python代码:
import numpy as np my_list = [1,2,3,4,5] my_array = np.array(my_list)
- 使用VS Code断点调试时,列表可正常展开查看所有元素,但Numpy数组无法像列表一样展开;展开其
T属性会无限循环显示自身。 - 问题始于切换至Python扩展预发布版本,切回正式版后仍未解决,同时出现Jedi语言服务器初始化失败、语法错误等异常日志。
VS Code版本信息
Version: 1.85.0 (user setup) Commit: af28b32d7e553898b2a91af498b1fb666fdebe0c Date: 2023-12-06T20:48:09.019Z Electron: 25.9.7 ElectronBuildId: 25551756 Chromium: 114.0.5735.289 Node.js: 18.15.0 V8: 11.4.183.29-electron.0 OS: Windows_NT x64 10.0.19045
相关错误日志
2023-12-15 11:41:41.416 [info] [Error - 11:41:41 AM] Server initialization failed. 2023-12-15 11:41:41.417 [info] Message: Pending response rejected since connection got disposed Code: -32097 2023-12-15 11:41:41.417 [info] [Info - 11:41:41 AM] Connection to server got closed. Server will restart. 2023-12-15 11:41:41.417 [info] true 2023-12-15 11:41:41.418 [info] [Error - 11:41:41 AM] Python Jedi client: couldn't create connection to server. 2023-12-15 11:41:41.418 [info] Message: Pending response rejected since connection got disposed Code: -32097 2023-12-15 11:41:41.419 [info] [Error - 11:41:41 AM] Restarting server failed 2023-12-15 11:41:41.419 [info] Message: Pending response rejected since connection got disposed Code: -32097 2023-12-15 11:41:41.419 [info] [Error - 11:41:41 AM] Server process exited with code 1. 2023-12-15 11:41:41.797 [info] Traceback (most recent call last): File "c:\Users\felix\.vscode\extensions\ms-python.python-2023.22.1\pythonFiles\run-jedi-language-server.py", line 9, in <module> from jedi_language_server.cli import cli File "c:\Users\felix\.vscode\extensions\ms-python.python-2023.22.1\pythonFiles\lib\jedilsp\jedi_language_server\__init__.py", line 5, in <module> from importlib_metadata import version File "c:\Users\felix\.vscode\extensions\ms-python.python-2023.22.1\pythonFiles\lib\jedilsp\importlib_metadata\__init__.py", line 6, in <module> import zipp File "c:\Users\felix\.vscode\extensions\ms-python.python-2023.22.1\pythonFiles\lib\jedilsp\zipp\__init__.py", line 9, in <module> from .py310compat import text_encoding File "c:\Users\felix\.vscode\extensions\ms-python.python-2023.22.1\pythonFiles\lib\jedilsp\zipp\py310compat.py", line 5 def _text_encoding(encoding, stacklevel=2, /): # pragma: no cover ^ SyntaxError: invalid syntax 2023-12-15 11:41:41.817 [info] [Error - 11:41:41 AM] Server process exited with code 1. 2023-12-15 11:41:41.819 [info] [Error - 11:41:41 AM] Server initialization failed. 2023-12-15 11:41:41.819 [info] Message: Pending response rejected since connection got disposed Code: -32097 2023-12-15 11:41:41.819 [info] [Error - 11:41:41 AM] The Python Jedi server crashed 5 times in the last 3 minutes. The server will not be restarted. See the output for more information. 2023-12-15 11:41:41.819 [info] [Error - 11:41:41 AM] Python Jedi client: couldn't create connection to server. 2023-12-15 11:41:41.819 [info] Message: Pending response rejected since connection got disposed Code: -32097 2023-12-15 11:41:41.819 [info] [Error - 11:41:41 AM] Restarting server failed 2023-12-15 11:41:41.819 [info] Message: Pending response rejected since connection got disposed Code: -32097
解决方案
从错误日志可知,核心问题是Jedi语言服务器依赖的zipp库使用了Python 3.10+专属的位置-only参数语法(/),但当前VS Code关联的Python解释器版本低于3.10,导致语法错误。结合调试numpy数组的问题,可按以下步骤处理:
- 切换Python解释器版本
按下Ctrl+Shift+P打开命令面板,输入Python: Select Interpreter,选择版本≥3.10的解释器。
- 切换Python解释器版本
- 重置Python扩展
卸载VS Code中的Python扩展,手动删除扩展缓存目录C:\Users\felix\.vscode\extensions\ms-python.python-*,重启VS Code后重新安装Python扩展正式版。
- 重置Python扩展
- 优化调试配置
在.vscode/launch.json中添加以下配置,让调试器正确解析numpy数组:
"configurations": [ { "name": "Python: Current File", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal", "justMyCode": false, "env": {"PYDEVD_DISABLE_FILE_VALIDATION": "1"} } ]- 优化调试配置
- 替换语言服务器
打开VS Code设置,搜索Python: Language Server,选择Pylance替代Jedi,避免Jedi的版本兼容问题。
- 替换语言服务器
内容的提问来源于stack exchange,提问作者Akut Luna
相关产品推荐
相关产品推荐

