Python3 VScode
在 VS Code 中安装 Python 扩展、创建并运行调试 Python 文件。
安装 Python 扩展
Search the marketplace for Python (published by Microsoft). It pulls in Pylance for completion, type hints and go-to-definition. Add Ruff or Black for formatting and linting.
- Python — core extension, run/debug/test support
- Pylance — fast language server with type checking
- Ruff / Black — linting and formatting
选择解释器
Press Ctrl+Shift+P and run "Python: Select Interpreter" to pick the project venv. The status bar shows the active interpreter; clicking it switches. A wrong interpreter is the most common cause of "it runs the wrong version".
import sys
print(bool(sys.executable))
print(sys.version_info.major)
print(bool(sys.base_prefix))True
3
True创建一个 Python 代码文件
Create hello.py; VS Code detects Python and enables highlighting and completion. Save, then click the ▷ button or run python hello.py in the integrated terminal.
import sys
def greet(name: str) -> str:
return f"Hello, {name}!"
if __name__ == "__main__":
print(greet("VS Code"))
print("argv is list:", isinstance(sys.argv, list))Hello, VS Code!
argv is list: True运行与调试
Ctrl+F5 runs without debugging; F5 starts the debugger. The first time it asks for the environment (Python File) and writes .vscode/launch.json, where you can set args, env vars and cwd.
import json
cfg = {
"version": "0.2.0",
"configurations": [{
"name": "Python: Current File",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
}],
}
c = cfg["configurations"][0]
print(c["type"], c["request"])
print(c["program"] == "${file}")
print(len(cfg["configurations"]))debugpy launch
True
1更多相关工具
Day to day you will also want pytest for tests, jupyter for exploratory work, mypy for static typing and ruff for lint plus format. VS Code has extensions for all of them.
import importlib.util
for name in ('pytest', 'mypy', 'ruff', 'jupyter'):
print(name, bool(importlib.util.find_spec(name)))pytest False
mypy False
ruff False
jupyter Falsepip install pytest mypy ruff. False is expected here.安装 Python 扩展
在扩展市场搜索 Python(Microsoft 官方),装上后会自动带上 Pylance 语言服务 —— 提供补全、类型提示、跳转定义。再装 Ruff 或 Black 做格式化与检查。
- Python —— 核心扩展,运行/调试/测试支持
- Pylance —— 快速语言服务与类型检查
- Ruff / Black —— 检查与格式化
选择解释器
按 Ctrl+Shift+P 执行「Python: Select Interpreter」,选中项目的 venv。状态栏左下角会显示当前解释器,点一下就能切换 —— 这是最常见的「代码跑错版本」原因。
import sys
print(bool(sys.executable))
print(sys.version_info.major)
print(bool(sys.base_prefix))True
3
True创建一个 Python 代码文件
新建 hello.py,VS Code 会识别为 Python 并启用语法高亮与补全。保存后点右上角的 ▷ 运行,或在集成终端里执行 python hello.py。
import sys
def greet(name: str) -> str:
return f"Hello, {name}!"
if __name__ == "__main__":
print(greet("VS Code"))
print("argv is list:", isinstance(sys.argv, list))Hello, VS Code!
argv is list: True运行与调试
Ctrl+F5 直接运行,F5 进入调试。第一次调试会让你选环境(Python File),之后生成 .vscode/launch.json,可以在里面配参数、环境变量和工作目录。
import json
cfg = {
"version": "0.2.0",
"configurations": [{
"name": "Python: Current File",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
}],
}
c = cfg["configurations"][0]
print(c["type"], c["request"])
print(c["program"] == "${file}")
print(len(cfg["configurations"]))debugpy launch
True
1更多相关工具
日常开发还会用到:pytest 跑测试、jupyter 做探索式分析、mypy 做静态类型检查、ruff 一次搞定 lint 与格式化。VS Code 都有对应扩展。
import importlib.util
for name in ('pytest', 'mypy', 'ruff', 'jupyter'):
print(name, bool(importlib.util.find_spec(name)))pytest False
mypy False
ruff False
jupyter Falsepip install pytest mypy ruff 单独安装。返回 False 是正常的。