You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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解释器

  1. 打开VS Code终端,执行pixi shell进入Pixi环境
  2. 在该会话中执行where python(Windows)或which python(Linux/macOS),获取Pixi环境的Python路径
  3. 按下Ctrl+Shift+P打开命令面板,选择Python: Select Interpreter,粘贴上述路径
  4. 此后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:手动启动调试服务器并附加

适用于特殊场景:

  1. 打开VS Code终端,执行pixi shell进入Pixi环境
  2. 运行调试服务器:
    python -m debugpy --listen 5678 --wait-for-client ./script.py
    
  3. 在.vscode/launch.json中添加附加配置:
    {
        "name": "Python: Attach to Pixi",
        "type": "python",
        "request": "attach",
        "connect": {
            "host": "localhost",
            "port": 5678
        }
    }
    
  4. 选择该附加配置启动调试,即可连接到Pixi环境中的调试进程

内容的提问来源于stack exchange,提问作者tmlen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 14:25:08