我在我的服务器上安装了不同的python版本(来自源代码)。现在我希望能够从另一个python脚本(运行另一个python版本)获取python可执行文件的版本。
我知道我可以用path/to/python -V
从shell中做到这一点。但是我想从脚本中做到这一点,比如:
command = ' '.join([pythonpath, '-V'])
output = subprocess.check_output( command, shell=True )
print output
但在这种情况下,check_output无法按预期工作:输出显示在终端中,但不会进入变量输出。
码:
#!/usr/bin/python2
# -*- coding: utf-8 -*-
from subprocess import Popen, PIPE
if __name__ == '__main__':
cmd = '/usr/local/bin/python2'
param = '-V'
process = Popen([cmd, param], stdout=PIPE, stderr=PIPE)
process.wait()
# be aware, the order should be out, err for shell tools, but python reply shows in err, out, i've no idea why
err, out = process.communicate()
print out
输出:
Python 2.7.15