You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS Code编写Python代码时无语法高亮问题求助

VS Code Python语法高亮失效解决办法

问题情况

VS Code中编写Python文件时突然几乎无语法高亮,无明确触发操作。
问题代码截图

已尝试以下操作但未解决:

  • 更换主题
  • 在settings.json中配置"python.languageServer": "Pylance"

当前完整settings.json配置:

{
    "[python]": {
        "editor.formatOnType": true
    },
    "git.autofetch": true,
    "explorer.confirmDelete": false,
    "git.confirmSync": false,
    "window.autoDetectColorScheme": true,
    "python.languageServer": "Pylance",
    "workbench.colorTheme": "Visual Studio Dark"
}

解决步骤

  • 重新加载窗口:按下Ctrl+Shift+P(Windows/Linux)或Cmd+Shift+P(Mac),输入Reload Window执行,强制重启VS Code服务。
  • 确认文件关联:查看右下角状态栏的语言模式,确保当前文件被识别为Python;若不是,点击语言模式选择"Python",或在settings.json中添加:
    "files.associations": {"*.py": "python"}
    
  • 清除Pylance缓存:删除Pylance缓存目录后重启VS Code:
    • Windows:%USERPROFILE%\.vscode\extensions\ms-python.vscode-pylance-*\server
    • Mac/Linux:~/.vscode/extensions/ms-python.vscode-pylance-*/server
  • 排查扩展冲突:临时禁用所有非Python相关扩展,逐个重新启用,排查是否有扩展导致冲突。
  • 重置VS Code设置:若以上无效,备份settings.json后,执行Preferences: Reset Settings命令重置设置,再重新配置Python相关选项。

内容的提问来源于stack exchange,提问作者Andreas Ravnholt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 13:42:09