Docker容器内R脚本调用Python脚本提示文件不存在的解决求助
问题:Docker容器中R脚本调用Python脚本提示文件找不到
环境与执行流程
Docker容器内根目录(/)下存在以下关键文件:
run_DMR_calling.py:入口脚本,会调用methylKit.RmethylKit.R:R脚本,内部通过system()调用Python脚本filter_parse.pyfilter_parse.py:目标Python脚本,位于根目录
执行流程:
- 运行
run_DMR_calling.py,该脚本调用methylKit.R methylKit.R中执行以下代码调用Python脚本:
###### run filter and unfiltered files filename <- paste0(bioset_folder, "/", biosetFile_name, ".txt") cmd <- paste0('python filter_parse.py out.',args$genome,'_refseq.tsv "', filename,'"') system(cmd)
报错信息
python: can't open file 'filter_parse.py': [Errno 2] No such file or directory
补充验证信息
- 容器内
pwd输出为/,确认脚本都在根目录 find / -name "filter_parse.py"确认文件路径为/filter_parse.py- 容器内Python版本为
Python 2.7.16 :: Anaconda, Inc.
解决方案
方案1:使用绝对路径调用Python脚本
修改R脚本中的命令,直接指定filter_parse.py的绝对路径,彻底规避工作目录问题:
cmd <- paste0('python /filter_parse.py out.',args$genome,'_refseq.tsv "', filename,'"')
方案2:检查并切换R脚本的工作目录
R脚本的system()调用会继承当前R进程的工作目录,可能不是根目录。先在R脚本中打印当前工作目录确认:
print(getwd())
如果确实不在根目录,可先切换工作目录再执行命令:
setwd("/") cmd <- paste0('python filter_parse.py out.',args$genome,'_refseq.tsv "', filename,'"') system(cmd)
方案3:确保Python脚本的权限(可选)
从ls -l输出看filter_parse.py权限是-rw-r--r--,虽然用python命令调用不需要执行权限,但如果后续需要直接运行脚本,可添加执行权限:
chmod +x /filter_parse.py
内容的提问来源于stack exchange,提问作者PesKchan
相关产品推荐
相关产品推荐

