通过PBS提交任务时Conda被当作Bash脚本执行的问题排查
问题:PBS任务中Conda被当作Bash脚本执行报错
提交的PBS任务脚本
qsub -N $JOBNAME -S /bin/bash -j y -o ~/Projects/JOBS/$JOBNAME.out << EOF #!/bin/bash echo "/////////////////////////////////////" # 初始化conda source ~/anaconda3/etc/profile.d/conda.sh # 检查conda是否激活成功 which python which conda conda list echo "/////////////////////////////////////" EOF
任务执行后的错误日志
alexmarc@lome /S/a/t/JOBS> cat reinventTest.out (reinvent4) ///////////////////////////////////// /bin/python ~/tools/anaconda3/condabin/conda ~/tools/anaconda3/bin/conda: line 3: import: command not found ~/tools/anaconda3/bin/conda: line 6: syntax error near unexpected token `sys.argv' ~/tools/anaconda3/bin/conda: line 6: `if len(sys.argv) > 1 and sys.argv[1].startswith('shell.') and sys.path and sys.path[0] == '':' /////////////////////////////////////
本地Shell执行的正常结果
alexmarc@lome /S/a/t/P/r/fromDost> which python ; which conda (base) ~/tools/anaconda3/bin/python ~/tools/anaconda3/bin/conda alexmarc@lome /S/a/t/P/r/fromDost>
已排查信息
- Conda可执行文件头部的shebang指向base环境的Python:
alexmarc@lome ~/JOBS> head ~/tools/anaconda3/bin/conda (base) #!~/tools/anaconda3/bin/python # -*- coding: utf-8 -*- import sys # Before any more imports, leave cwd out of sys.path for internal 'conda shell.*' commands. if len(sys.argv) > 1 and sys.argv[1].startswith('shell.') and sys.path and sys.path[0] == '': # The standard first entry in sys.path is an empty string, # and os.path.abspath('') expands to os.getcwd(). del sys.path[0] - 确认任务执行前后PATH未发生变化,Conda和Python路径已正确配置
原因分析
- shebang中的相对路径无法被PBS解析:Conda可执行文件第一行的
#!~/tools/anaconda3/bin/python使用了~表示用户主目录,但PBS作业的执行环境通常不支持在shebang中解析~,导致系统找不到对应的Python解释器,只能默认用Bash执行这个Python脚本,从而出现import命令未找到、Python语法被当作Bash命令的错误。 - PATH优先级问题:任务中
which conda返回~/tools/anaconda3/condabin/conda,而本地是~/tools/anaconda3/bin/conda,说明PBS环境里condabin目录在PATH中的优先级更高。condabin/conda是一个Shell包装脚本,需要调用正确的Python环境才能工作,但此时系统Python(/bin/python)在PATH中优先级高于Conda的Python,导致包装脚本调用失败,触发错误。
解决方法
方法1:修改Conda可执行文件的shebang为绝对路径
编辑~/tools/anaconda3/bin/conda文件,将第一行的相对路径shebang替换为Python解释器的绝对路径。可通过realpath ~/tools/anaconda3/bin/python获取绝对路径,示例:
#!/home/alexmarc/tools/anaconda3/bin/python
方法2:在PBS脚本中显式激活Conda Base环境
修改PBS脚本,在source conda.sh之后显式激活base环境,确保环境变量正确加载:
qsub -N $JOBNAME -S /bin/bash -j y -o ~/Projects/JOBS/$JOBNAME.out << EOF #!/bin/bash echo "/////////////////////////////////////" # 初始化conda并激活base环境 source ~/tools/anaconda3/etc/profile.d/conda.sh conda activate base # 检查环境是否正确加载 which python which conda conda list echo "/////////////////////////////////////" EOF
方法3:手动设置PATH优先加载Conda目录
在PBS脚本开头,将Conda的bin目录添加到PATH最前面,确保Conda的Python和可执行文件优先级高于系统版本:
qsub -N $JOBNAME -S /bin/bash -j y -o ~/Projects/JOBS/$JOBNAME.out << EOF #!/bin/bash echo "/////////////////////////////////////" # 优先加载conda的bin目录 export PATH=~/tools/anaconda3/bin:$PATH # 初始化conda source ~/tools/anaconda3/etc/profile.d/conda.sh # 检查环境 which python which conda conda list echo "/////////////////////////////////////" EOF
内容的提问来源于stack exchange,提问作者Alex Plastow
相关产品推荐
相关产品推荐

