从脚本获取 python 可执行文件的版本
Geting version of python executable from a script
我的服务器上安装了不同的 python 版本(来自源代码)。现在我希望能够从另一个 python 脚本(运行 另一个 python 版本)获取 python 可执行文件的版本。
我知道我可以从 shell 和 path/to/python -V
做到这一点。但我想通过脚本来完成,例如:
command = ' '.join([pythonpath, '-V'])
output = subprocess.check_output( command, shell=True )
print output
但在这种情况下 check_output 无法按预期工作:输出显示在终端中但不会进入变量 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
您可以查看源代码here。
我的服务器上安装了不同的 python 版本(来自源代码)。现在我希望能够从另一个 python 脚本(运行 另一个 python 版本)获取 python 可执行文件的版本。
我知道我可以从 shell 和 path/to/python -V
做到这一点。但我想通过脚本来完成,例如:
command = ' '.join([pythonpath, '-V'])
output = subprocess.check_output( command, shell=True )
print output
但在这种情况下 check_output 无法按预期工作:输出显示在终端中但不会进入变量 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
您可以查看源代码here。