VSCode 安装扩展
介绍 Visual Studio Marketplace 六大常见扩展类别,并以安装微软官方 Python 扩展为例演示搜索安装、创建 hello.py 以及启用 Python 智能感知补全的完整流程。
学完这一章,你能够
- 介绍 Visual Studio Marketplace 六大常见扩展类别,并以安装微软官方 Python 扩展为例演示搜索安装、创建 hello.py 以及启用 Python 智能感知补全的完整流程。
扩展生态系统与 Visual Studio Marketplace
高度可扩展的插件体系是 VS Code 的核心竞争力。Visual Studio Marketplace(`https://marketplace.visualstudio.com/`)汇聚了海量官方与社区扩展,典型分为六大类:编程语言支持(语法高亮、补全与类型检查)、调试工具、版本控制增强(如 GitLens)、主题与图标美化、工程与云原生工具集成(容器/云服务),以及 API 文档、代码片段与快速修复等辅助开发插件。
打开扩展视图并安装微软 Python 扩展
下面以为 VS Code 增加 Python 开发能力为例:点击活动栏中的“扩展视图(Extensions View)”图标(快捷键 `Ctrl+Shift+X` / `Cmd+Shift+X`),即可在编辑器内直接检索和管理插件。
在顶部搜索栏输入 `Python`,选中发布者为 `Microsoft` 的官方 Python 扩展(扩展 ID 为 `ms-python.python`),点击 Install(安装)按钮等待自动部署完成。
编写 hello.py 体验 Python 智能感知
安装扩展后,在工作区新建 `hello.py` 并编写定义 `say_hello(name)`、打印 `"Hello, " + name` 并传入 `"VS Code"` 调用的示例程序。此时编辑器已为 Python 代码激活代码建议(Suggestions)、智能感知(IntelliSense)与语法高亮。本站离线脚本已校验六大扩展分类、`.vscode/extensions.json` 标识格式,并在本地实际运行 `hello.py` 输出 `Hello, VS Code` 且提取了 AST 函数签名。
运行扩展标识与分类规范校验以及 hello.py 本地执行与 AST 符号提取
展开查看收起
"""Offline VS Code Extensions Marketplace metadata & Python hello.py verifier for Ch16."""
import ast
import json
import re
import subprocess
import sys
import tempfile
from pathlib import Path
VERSION = "CPython 3.12.14 offline VS Code Extensions & Python IntelliSense verifier"
def check(name, summary, expected, actual):
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = []
# Case 1: Marketplace extension ID validation, 6 categories, and .vscode/extensions.json
categories = ["编程语言支持", "调试工具", "版本控制", "主题和美化", "工具集成", "辅助开发"]
ext_json = {
"recommendations": [
"ms-python.python",
"eamodio.gitlens",
"esbenp.prettier-vscode",
]
}
id_pat = re.compile(r"^[a-z0-9-]+\.[a-z0-9-]+$", re.I)
all_valid = all(bool(id_pat.match(e)) for e in ext_json["recommendations"])
cases.append(
check(
"marketplace_catalog_and_extension_id",
"Verify 6 VS Code extension categories and valid publisher.name identifiers in extensions.json",
{
"category_count": 6,
"recommendation_count": 3,
"all_ids_valid": True,
"includes_ms_python": True,
},
{
"category_count": len(categories),
"recommendation_count": len(ext_json["recommendations"]),
"all_ids_valid": all_valid,
"includes_ms_python": "ms-python.python" in ext_json["recommendations"],
},
)
)
# Case 2: Write and run hello.py + extract AST function signature for IntelliSense
hello_code = (
"def say_hello(name):\n"
' print("Hello, " + name)\n\n'
'say_hello("VS Code")\n'
)
tree = ast.parse(hello_code)
funcs = [
{"name": n.name, "args": [a.arg for a in n.args.args]}
for n in tree.body
if isinstance(n, ast.FunctionDef)
]
with tempfile.TemporaryDirectory() as tmp:
py_file = Path(tmp) / "hello.py"
py_file.write_text(hello_code, encoding="utf-8")
proc = subprocess.run(
[sys.executable, "-I", str(py_file)],
capture_output=True,
encoding="utf-8",
timeout=15,
)
cases.append(
check(
"python_extension_hello_py_exec",
"Verify hello.py execution output and Python AST symbol extraction for IntelliSense",
{
"function_name": "say_hello",
"function_args": ["name"],
"returncode": 0,
"stdout": "Hello, VS Code",
},
{
"function_name": funcs[0]["name"],
"function_args": funcs[0]["args"],
"returncode": proc.returncode,
"stdout": proc.stdout.strip(),
},
)
)
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
市场中存在大量同名或第三方衍生插件,核对官方发布者(如 `ms-python.python`)可避免误装非官方或低质量扩展;团队可在项目根目录创建 `.vscode/extensions.json` 写入 `recommendations` 列表统一推荐。