如何在Pixi环境中运行Visual Studio Code Python调试器?
在Pixi环境中运行VS Code Python调试会话的方法
问题背景
使用Pixi管理Python项目环境时,需先执行pixi shell加载pyproject.toml定义的环境变量并运行指定.bat脚本,才能正常执行python.exe ./script.py。但VS Code调试时会创建未执行pixi shell的新终端,导致环境不匹配;手动在Pixi shell中运行调试命令则无法让IDE建立连接。
解决方案
方案1:指定Pixi环境的Python解释器
- 打开VS Code终端,执行
pixi shell进入Pixi环境 - 在该会话中执行
where python(Windows)或which python(Linux/macOS),获取Pixi环境的Python路径 - 按下
Ctrl+Shift+P打开命令面板,选择Python: Select Interpreter,粘贴上述路径 - 此后VS Code调试器会直接使用该Python解释器,自动加载Pixi配置的所有环境变量,无需手动执行
pixi shell
方案2:通过pixi run配置调试命令
若方案1无法覆盖.bat脚本的额外配置,可修改.vscode/launch.json,让调试器通过pixi run在Pixi环境中启动:
{ "version": "0.2.0", "configurations": [ { "name": "Python: Pixi Debug", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal", "command": "pixi", "args": [ "run", "python", "-m", "debugpy", "--listen", "${command:pickPort}", "--wait-for-client", "${file}" ] } ] }
- 该配置会调用
pixi run在Pixi环境中启动debugpy,自动执行环境变量配置和.bat脚本 - 调试时直接选择此配置启动即可
方案3:手动启动调试服务器并附加
适用于特殊场景:
- 打开VS Code终端,执行
pixi shell进入Pixi环境 - 运行调试服务器:
python -m debugpy --listen 5678 --wait-for-client ./script.py - 在
.vscode/launch.json中添加附加配置:{ "name": "Python: Attach to Pixi", "type": "python", "request": "attach", "connect": { "host": "localhost", "port": 5678 } } - 选择该附加配置启动调试,即可连接到Pixi环境中的调试进程
内容的提问来源于stack exchange,提问作者tmlen
相关产品推荐
相关产品推荐

