VS Code中Python/Jupyter Notebook f-string语法高亮异常问题咨询
Jupyter Notebook与Python文件f-string高亮异常问题排查求助
以下是Jupyter Notebook和两段Python代码片段的示例:
Jupyter中大多数情况下f-string无法正常高亮,仅当其中是单个变量时才会高亮,即便变量已定义,写成a+b这类表达式时就无法高亮。
Python文件中,单独定义的变量可以高亮,但变量搭配任何数学运算时就无法高亮,表现非常不一致。
我已检查过settings.json配置,认为问题并不出在配置上,我的配置如下:
{ "gitlens.advanced.messages": { "suppressShowKeyBindingsNotice": true }, "workbench.iconTheme": "Monokai Pro (Filter Spectrum) Icons", "liveServer.settings.donotShowInfoMsg": true, "explorer.confirmDelete": false, "editor.tabSize": 2, "editor.rulers": [110], "workbench.startupEditor": "none", "editor.snippetSuggestions": "top", "workbench.statusBar.visible": true, "editor.suggestSelection": "first", "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue", "python.pythonPath": "/opt/anaconda3/envs/abc/bin/python", "workbench.colorCustomizations": { "editorRuler.foreground": "#e1b525" }, "python.autoComplete.addBrackets": true, "editor.wordWrapColumn": 200, "python.formatting.provider": "black", "launch": { "configurations": [], "compounds": [] }, "workbench.colorTheme": "One Dark Pro", "python.linting.mypyEnabled": true, "python.linting.pylintEnabled": true, "python.linting.enabled": false, "html.format.enable": false, "javascript.format.enable": false, "typescript.format.enable": false, "json.format.enable": false, "python.languageServer": "Pylance", "python.analysis.completeFunctionParens": true, "kite.showWelcomeNotificationOnStartup": false, "jupyter.askForKernelRestart": false, "jupyter.textOutputLimit": 0, "jupyter.alwaysTrustNotebooks": true, "jupyter.sendSelectionToInteractiveWindow": true, "workbench.editorAssociations": { "*.ipynb": "jupyter.notebook.ipynb" }, "python.defaultInterpreterPath": "/opt/anaconda3/envs/abc/bin/python", "[python]": { "gitlens.codeLens.symbolScopes": [ "!Module" ], "editor.wordBasedSuggestions": false } }
我怀疑"python.formatting.provider": "black"这一项可能有影响,但据我了解它仅负责代码格式化,和高亮功能无关。
更新
我的Notebook版本为6.4.4,但仍然存在该高亮异常问题。
更新
我补充了使用其他主题时的截图和我的.json配置文件截图:
请问该f-string高亮异常问题应如何排查修复?
排查修复方案
你遇到的是VS Code环境下Pylance语言服务、Jupyter扩展与主题语法高亮规则兼容的典型问题,可按以下步骤逐一排查:
- 升级Pylance与Jupyter扩展
当前Pylance早期版本对f-string内部表达式的语义高亮支持存在缺陷,先将Pylance升级到2022.4.0及以上版本,同时将VS Code Jupyter扩展升级到v2022.3.1000901801及以上,这两个版本修复了大量f-string高亮适配问题。 - 验证语法高亮规则本身的兼容性
切换到VS Code默认的Dark+或Light+主题测试,如果默认主题下f-string内部表达式高亮正常,说明是当前使用的One Dark Pro主题的语法高亮规则未覆盖f-string内部复杂表达式场景,可更换为One Dark Pro Flat等更新维护的同风格主题,或者自行在settings.json中添加自定义高亮规则:
"editor.tokenColorCustomizations": { "[One Dark Pro]": { "textMateRules": [ { "scope": "meta.embedded.expression.python string.quoted.single.python, meta.embedded.expression.python string.quoted.double.python", "settings": { "foreground": "#e06c75" } }, { "scope": "meta.embedded.expression.python variable.other.readwrite.python", "settings": { "foreground": "#e5c07b" } } ] } }
- 调整语义高亮配置
在settings.json中添加以下配置,强制开启Python语义高亮:
"[python]": { "editor.semanticHighlighting.enabled": true }, "jupyter.experimental.toggleSemanticHighlighting": true
- 排查扩展冲突
如果仍未解决,临时禁用Kite、GitLens等可能注入代码高亮逻辑的扩展,逐一排查是否存在扩展冲突。
内容的提问来源于stack exchange,提问作者Jonas Palačionis
相关产品推荐
相关产品推荐

