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

Snakemake Shell命令中通过Shell变量访问Python字典值触发NameError的问题求助

Snakemake Shell命令中通过Shell变量访问Python字典值触发NameError的问题求助

各位好,我最近在写Snakemake规则的时候碰到了个棘手的问题:我尝试在shell命令里用Shell变量去访问Python字典的值,但运行时直接触发了NameError错误。

先给大家看一下我简化后的测试代码:

my_dict = {"SHELL_KEY": "key3"}

rule another_rule:
    output: "output.txt"
    shell:
        """
        x='SHELL_KEY'
        echo {my_dict[$x]} > {output}
        """

运行之后抛出的错误信息是这样的:

RuleException in rule another_rule in file /home/zhujack/workflow/ngs_aPipe_snake_dev/rules/align.rules, line 43:
NameError: The name '$x' is unknown in this context. \
  Please make sure that you defined that variable. \
  Also note that braces not used for variable access have \
  to be escaped by repeating them, i.e. {{print $1}}, \
  when formatting the following:

        x='SHELL_KEY'
        echo {my_dict[$x]} > {output}

错误提示说$x这个名字在当前上下文里不存在,但我明明在shell代码里定义了这个变量啊。有没有大佬能帮我分析下问题出在哪,应该怎么调整才能实现我想要的效果?

备注:内容来源于stack exchange,提问作者Jack Z

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 11:39:31