VSCode 必须手动指定 Poetry 虚拟环境中的真实 Python 解释器路径(如 macOS/Linux 下为 venv/bin/python,Windows 下为 venv\Scripts\python.exe),而非仅用 poetry env info --path 输出的根目录;需通过 “Enter interpreter path” 设置并固化于 .vscode/settings.json,调试时须用 poetry run 而非依赖 shell 激活。

VSCode 不会自动识别 Poetry 创建的虚拟环境,必须手动指定 python 可执行文件路径,否则 import 报错、调试失败、Pylance 类型提示失效——这不是插件问题,是解释器根本没对齐。
poetry env info --path 输出的不是解释器,只是起点
很多人复制 poetry env info --path 的输出(比如 /Users/me/Library/Caches/pypoetry/virtualenvs/myproj-abc123-py3.11)就直接粘贴进 VSCode,结果无效。这个路径只是虚拟环境根目录,不是 Python 解释器本身。
- macOS/Linux 下真实解释器是:
<path>/bin/python</path> - Windows 下真实解释器是:
<path>\Scripts\python.exe</path> - 必须在项目根目录下运行
poetry env info --path,否则返回的是其他项目的环境路径 - 别信
which python或poetry shell后终端里显示的路径——它可能被pyenv、shell alias 或旧PATH干扰
Python: Select Interpreter 必须选 “Enter interpreter path”
VSCode 默认扫描范围不包括 Poetry 的缓存目录(~/Library/Caches/pypoetry/virtualenvs/ 或 %LOCALAPPDATA%\pypoetry\Cache\virtualenvs\),所以列表里几乎不会出现 Poetry 环境。
- 按
Ctrl+Shift+P(Windows/Linux)或Cmd+Shift+P(macOS),输入并选择Python: Select Interpreter - 不要从下拉列表里“猜”,务必点击
Enter interpreter path - 粘贴拼好的完整路径,例如:
/Users/me/Library/Caches/pypoetry/virtualenvs/myproj-abc123-py3.11/bin/python - 输错路径(比如漏了
/bin/python)时,VSCode 会静默 fallback 到上一个解释器,且不报错——这是最常被忽略的失败点
.vscode/settings.json 里硬编码路径才能真正固化配置
每次切换 Git 分支、重装依赖、甚至重启 VSCode,都可能丢失解释器选择。靠手点或记忆不可靠,必须写死配置。
仅使用 Python 将 Claw Code 虚拟化框架与 OpenClaw 集成,提供 184 种工具和 200 多条命令,用于安全分析和代码质量...
立即学习“Python免费学习笔记(深入)”;
- 在项目根目录创建或编辑
.vscode/settings.json - 加入这一行(注意 Windows 用双反斜杠或正斜杠,路径必须和
poetry env info --path输出完全一致):"python.defaultInterpreterPath":"/full/path/to/venv/bin/python" - 保存后,必须关闭并重新打开整个 VSCode 窗口(不是 “Developer: Reload Window”),否则 Pylance 和 debugger 仍缓存旧环境
debug 配置需绕过 poetry shell,直接走 poetry run
VSCode 的调试器不继承终端激活状态,不能依赖 poetry shell 后再启动调试。必须显式通过 poetry run 执行目标脚本。
- 在
.vscode/launch.json中添加配置: "name": "Poetry: Current File""type": "python""request": "launch""module": "poetry""args": ["run", "python", "${file}"]"cwd": "${workspaceFolder}"
调试时选这个配置,断点才能命中,且所有依赖都来自当前 Poetry 环境——而不是你误选的系统 Python 或另一个 venv。

















