python(commands.getoutput)无法识别别名Linux命令

Sci*_*ion 1 python linux

我正在使用Ubuntu 12.4,我安装了matlab.通常,为了从终端调用matlab,我必须输入'〜/ MATLAB/bin/matlab'.显然这有点烦人所以我通过添加别名这个命令

alias matlab='sh ~/MATLAB/bin/matlab'
Run Code Online (Sandbox Code Playgroud)

到.bashrc.现在一切都很黄金,在终端(bash)中输入'matlab'可以在任何目录下运行.

当我试图从python脚本调用Matlab时出现问题.有这样的声明:

>>> commands.getoutput('matlab')
'sh: 1: matlab: not found'
Run Code Online (Sandbox Code Playgroud)

因为似乎别名没有得到承认.只想确认一下:

>>> commands.getoutput('~/MATLAB/bin/matlab')
Run Code Online (Sandbox Code Playgroud)

就像一个魅力,和

>>> commands.getoutput('echo $SHELL')
'/bin/bash'
Run Code Online (Sandbox Code Playgroud)

确实验证python正试图在bash中执行cmd ...

知道这里发生了什么吗?为什么别名被认可?如何/可以修复?

谢谢!

aay*_*ubi 5

matlab二进制路径添加到PATH环境变量中.

PATH=~/MATLAB/bin/:$PATH
export PATH
Run Code Online (Sandbox Code Playgroud)

然后python会发现matlab:

>>> commands.getoutput('matlab')
Run Code Online (Sandbox Code Playgroud)

commands不知道你的shell目前的别名.但是环境变量如PATH持久化.