gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:16:55 25 4
gpt4 key购买 nike

我正在使用 Ubuntu 12.4,并且安装了 matlab。通常,为了从终端调用 matlab,我必须输入“~/MATLAB/bin/matlab”。显然这有点烦人,所以我通过添加

为这个命令起别名
alias matlab='sh ~/MATLAB/bin/matlab'

到.bashrc。现在一切都很顺利,在终端 (bash) 中输入“matlab”可以在任何目录下工作。

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

>>> commands.getoutput('matlab')
'sh: 1: matlab: not found'

好像别名没有被识别。只是为了确保:

>>> commands.getoutput('~/MATLAB/bin/matlab')

像魅力一样工作,并且

>>> commands.getoutput('echo $SHELL')
'/bin/bash'

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

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

谢谢!

最佳答案

matlab 二进制路径添加到您的 PATH 环境变量。

PATH=~/MATLAB/bin/:$PATH
export PATH

然后python会找到matlab:

>>> commands.getoutput('matlab')

认为 commands 不知道您的 shell 当前的别名。但是 PATH 等环境变量仍然存在。

关于python - python (commands.getoutput) 无法识别带别名的 Linux 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11761300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com