Python不能执行终端脚本,使用SH也不庆典 [英] Python can't execute terminal script, using sh nor bash

查看:496
本文介绍了Python不能执行终端脚本,使用SH也不庆典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望你们能帮助我解决这个问题,因为我真的卡住了......我试图从执行的python程序,并出于某种原因,这是行不通的。该脚本位于:

i hope you guys can help me with this problem because i'm really stuck... I'm trying to execute a program from python and, for some reason, it doesn't work. The script is located at:

path/to/teqc

我已经添加了此行的.bashrc文件:

I've added this line to the .bashrc file:

alias teqc='path/to/teqc'

和,当我运行

TEQC -tr D输入>输出

在终端上正常工作......但是,如果我在一个Python程序运行它,它表明:

on a terminal it works fine... but, if i run it on a python program, it shows:

sh: teqc: command not found

在code我一直在使用Python的是:

the code i've been using on python is:

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

我试图用

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

,但唯一的结果是改变错误消息

but the only result was to change the error message to

/bin/bash: teqc: command not found

任何帮助将是非常美联社preciated:)

Any help would be really appreciated :)

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

P.D. I forgot to specify, the operating system is Fedora 21

推荐答案

我认为这个问题是环境变量PATH是不一样的,当你使用子在code运行命令。

I think the problem is that the environment variable PATH is not the same when you run the command in the code using subprocess.


  1. 一个解决办法是有作为在previous答案提出的软链接

  2. 您可以做其他的事情是让你的code你使用的子os模块来已可用于追加使用类似的路径os.environ字典执行命令之前,设置环境这种

  1. One solution would be to have a soft link in as suggested in the previous answer
  2. Other thing you can do is to have your code set the environment before you execute your command using subprocess the os module comes has a os.environ dictionary which can be used to append the path using something like this

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


这篇关于Python不能执行终端脚本,使用SH也不庆典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆