Python无法使用sh或bash执行终端脚本

时间:2015-01-14 20:23:19

标签: python linux bash sh fedora

我希望你们可以帮助我解决这个问题,因为我真的被卡住了...我正在尝试从python执行一个程序,并且由于某种原因,它不起作用。该脚本位于:

path/to/teqc

我已将此行添加到.bashrc文件中:

alias teqc='path/to/teqc'

,当我跑

teqc -tr d input >output

在终端上工作正常......但是,如果我在python程序上运行它,它会显示:

sh: teqc: command not found

我在python上使用的代码是:

os.system('teqc -tr d input >output')

我尝试使用

subprocess.Popen('teqc -tr d input >output', shell=True, executable="/bin/bash")

但唯一的结果是将错误消息更改为

/bin/bash: teqc: command not found

任何帮助都会非常感激:)

Pd积。我忘了指定,操作系统是Fedora 21

2 个答案:

答案 0 :(得分:2)

我建议您创建一个指向您程序的符号链接。

ln -s /path/to/teqc /usr/bin/teqc

答案 1 :(得分:0)

我认为问题是当您使用子进程在代码中运行命令时,环境变量PATH不一样。

  1. 一种解决方案是按照上一个答案中的建议设置软链接
  2. 您可以做的其他事情是在使用子进程执行命令之前让您的代码设置环境os模块带有一个os.environ字典,可用于使用类似的东西追加路径

    import os
    import subprocess
    os.environ['PATH'] += ":/path/to/teqc"
    subprocess.Popen(['teqc -tr d input'],stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)