如果您需要的别名是在~/.bashrc中定义的,那么它不会运行,原因如下:
1)必须给“shell”关键字arg:
subprocess.call('command', shell=True)
否则,给定的命令将用于查找可执行文件,而不是传递给shell,而shell将扩展别名和函数等内容。
2)默认情况下,subprocess.call和friends使用'/bin/sh'外壳。如果这是要调用的bash别名,则需要使用“executable”关键字arg告诉子进程使用bash而不是sh:
subprocess.call('command', shell=True, executable='/bin/bash')
3)但是,/bin/bash不会源代码~/.bashrc,除非以“交互式”shell(带“-i”)启动。不幸的是,不能传递executable='/bin/bash-i',因为它认为整个值是可执行文件的名称。因此,如果您的别名是在用户的正常交互启动中定义的,例如在.bashrc中,那么您将不得不使用以下替代形式调用该命令:
subprocess.call(['/bin/bash', '-i', '-c', command])
# i.e. shell=False (the default)