VSCode中文件过多时Pylance代码高亮失效问题求助
针对项目文件增多后Pylance代码高亮失效的问题,可尝试以下几种方案:
优化目录排除规则
先修正files.watcherExclude里的拼写错误:把"**/ouput/**"改成"**/output/**",否则该目录无法被正确排除。同时补充排除Python编译文件和缓存目录,减少Pylance的分析负载,比如在python.analysis.exclude和files.watcherExclude中加入**/__pycache__/**、**/*.pyc。提高Pylance内存限制
在settings.json中添加"python.analysis.memoryLimit": 8192(单位为MB,可根据你的机器内存调整,比如16G内存可设为12288),避免Pylance因内存不足导致功能异常。关闭非必需分析特性
如果不需要严格的类型检查,可将python.analysis.typeCheckingMode设为"off",或者关闭python.analysis.inlayHints等非核心功能,减轻Pylance的运行压力。快速重启语言服务
出现高亮失效时,不用删除项目,按Ctrl+Shift+P(Windows/Linux)或Cmd+Shift+P(Mac),输入Python: Restart Language Server即可快速恢复,这是临时解决的高效方式。拆分大项目为多根工作区
若项目规模过大,可拆分为多个子项目,使用VSCode的多根工作区功能,让Pylance仅分析当前活跃的子项目,从根源降低分析负载。
修正后的settings.json示例:
{ "remote.autoForwardPortsSource": "output", "python.analysis.completeFunctionParens": true, "python.languageServer": "Pylance", "python.analysis.diagnosticMode": "workspace", "python.analysis.memoryLimit": 8192, "python.analysis.exclude": [ "**/data/**", "**/tmp/**", "**/output/**", "**/dataset/**", "**/ckpts/**", "**/work_dirs/**", "**/assets/**", "**/docs/**", "**/figs/**", "**/resources/**", "**/.git/**", "**/.github/**", "**/__pycache__/**", "**/*.pyc", "**/*.pyo" ], "files.watcherExclude": { "**/.git/**": true, "**/tmp/**": true, "**/output/**": true, "**/others/**": true, "**/data/**": true, "**/.github/**": true, "**/__pycache__/**": true, "**/*.pyc": true }, "explorer.autoReveal": false }
内容的提问来源于stack exchange,提问作者Park Bo

