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

如何在Visual Studio Code中调试Snakemake Snakefile?断点无法暂停

解决Snakemake调用Python脚本时VS Code断点不生效的问题

问题根源

你在Snakefile中使用shell('python printhello.py')启动了独立的Python子进程,而VS Code调试器仅附着在Snakemake的主Python进程上,无法自动关联到子进程,因此断点不会触发。

解决方案

方案1:用script指令替代shell调用(推荐)

使用Snakemake原生的script指令调用Python脚本,脚本会在Snakemake的主Python环境中执行,调试器可直接捕获:
修改Snakefile:

rule all:
    input: "hello/world.txt"

rule hello_world:
    output: "hello/world.txt"
    script: "printhello.py"

保持原launch.json配置不变(确保justMyCode设为false),直接启动SnakeDebug调试配置,断点即可正常触发。

方案2:将脚本代码嵌入Snakefile

若脚本逻辑简单,可直接把代码放到Snakefile的run块中,代码会在主进程执行,断点直接生效:

rule all:
    input: "hello/world.txt"

rule hello_world:
    output: "hello/world.txt"
    run:
        print('hello world! file output')
        pt = 1  # 在此处设置断点即可
        print('After break point.')
        # 手动生成输出文件
        with open("hello/world.txt", "w") as f:
            f.write("hello content")

方案3:通过subprocess传递调试参数(适合复杂场景)

修改Snakefile的run块,用subprocess调用脚本并传递调试参数,让子进程被主调试器附着:

rule all:
    input: "hello/world.txt"

rule hello_world:
    output: "hello/world.txt"
    run:
        import subprocess
        import sys
        # 传递调试参数,等待调试器连接
        debug_cmd = [sys.executable, "-m", "debugpy", "--listen", "localhost:5678", "--wait-for-client", "printhello.py"]
        subprocess.run(debug_cmd)

在launch.json中新增一个附着配置:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "SnakeDebug",
            "type": "python",
            "request": "launch",
            "program": "C:/AV/pypsa_fes_mamba/pypsa_fes/Scripts/snakemake.exe",
            "args": [
                "--snakefile","pipeline1_01.txt",
                "--cores","1"
            ],
            "cwd":"${workspaceFolder}",
            "pythonPath": "C:/AV/pypsa_fes_mamba/pypsa_fes/python.exe",
            "console": "integratedTerminal",
            "justMyCode": false,
            "trace":"verbose"
        },
        {
            "name": "Attach to Subprocess",
            "type": "python",
            "request": "attach",
            "connect": {
                "host": "localhost",
                "port": 5678
            },
            "justMyCode": false
        }
    ]
}

调试步骤:

  1. 启动SnakeDebug配置,此时Snakemake会等待调试器连接
  2. 启动Attach to Subprocess配置,调试器即可附着到子进程,断点触发

注意事项

  • 方案3需要先安装debugpy:pip install debugpy
  • 确保launch.json中的pythonPath与Snakemake使用的Python环境一致

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 18:53:34