Nextflow自定义模块中Python脚本执行找不到,引用方式是否正确?
问题:Nextflow模块中Python脚本无法找到的排查与解决
问题背景
开发基于自定义模块的Nextflow流程,模块内的Python脚本script_1.py位于modules/local/mymodule/resources/usr/bin/目录下,已设置可执行权限,且在根目录的nextflow.config中启用了nextflow.enable.moduleBinaries=true,但运行流程时始终提示找不到该脚本。
模块目录结构
modules/ └── local/ └── mymodule/ ├── environment.yml ├── main.nf ├── resources/ │ └── usr/ │ └── bin/ │ └── script_1.py └── work/
错误信息
Caused by: Process `MyProcess (1)` terminated with an error exit status (2) Command executed: python script_1.py cat <<-END_VERSIONS > versions.yml "MyProcess": python: $(python --version 2>&1 | sed 's/Python //g') END_VERSIONS Command exit status: 2 Command output: (empty) Command error: python: can't open file 'script_1.py': [Errno 2] No such file or directory
已尝试的流程代码
主流程main.nf
#!/usr/bin/env nextflow include { MyProcess } from './modules/local/mymodule/main.nf'
模块内main.nf
#!/usr/bin/env nextflow process MyProcess{ conda "${moduleDir}/environment.yml" input: path(input_folder) output: path("data.csv") path "versions.yml" , emit: versions script: """ python script_1.py ${input_folder} cat <<-END_VERSIONS > versions.yml "${task.process}": python: \$(python --version 2>&1 | sed 's/Python //g') END_VERSIONS """ }
问题解答
你当前引用脚本的方式不正确,核心原因及解决方法如下:
原因分析
启用nextflow.enable.moduleBinaries=true后,Nextflow会自动将模块resources/usr/bin目录添加到任务的PATH环境变量中,但你当前用python script_1.py的写法,是让Python在任务的当前工作目录查找脚本,而非通过PATH定位脚本所在目录。
解决方法
提供两种可行方案:
- 直接调用脚本(推荐)
由于脚本已设置可执行权限,且所在目录已被加入PATH,直接调用脚本文件名即可。需确保script_1.py开头有shebang声明(#!/usr/bin/env python),修正后的脚本块如下:script: """ script_1.py ${input_folder}
cat <<-END_VERSIONS > versions.yml
"${task.process}":
python: $(python --version 2>&1 | sed 's/Python //g')
END_VERSIONS
"""
2. **通过绝对路径调用** 如果必须保留`python`前缀,可利用Nextflow内置的`moduleDir`变量(指向模块根目录)拼接脚本的绝对路径: ```nextflow script: """ python ${moduleDir}/resources/usr/bin/script_1.py ${input_folder} cat <<-END_VERSIONS > versions.yml "${task.process}": python: \$(python --version 2>&1 | sed 's/Python //g') END_VERSIONS """
额外检查项
- 确认
script_1.py确实拥有可执行权限,可通过chmod +x modules/local/mymodule/resources/usr/bin/script_1.py设置; - 确认使用的Nextflow版本为22.10.0或更高(
nextflow.enable.moduleBinaries特性从此版本开始支持)。
内容的提问来源于stack exchange,提问作者Rea Kalampaliki
相关产品推荐
相关产品推荐

