从脚本获取python可执行文件的版本

从脚本获取python可执行文件的版本,python,subprocess,version,Python,Subprocess,Version,我的服务器上安装了不同的python版本(源代码)。现在,我希望能够从另一个python脚本(运行另一个python版本)获取python可执行文件的版本 我知道我可以在shell中使用path/to/python-V完成这项工作。但我想从脚本开始,比如: command = ' '.join([pythonpath, '-V']) output = subprocess.check_output( command, shell=True ) print output 但在这种情况下,检查输出

我的服务器上安装了不同的python版本(源代码)。现在,我希望能够从另一个python脚本(运行另一个python版本)获取python可执行文件的版本

我知道我可以在shell中使用
path/to/python-V
完成这项工作。但我想从脚本开始,比如:

command = ' '.join([pythonpath, '-V'])
output = subprocess.check_output( command, shell=True )
print 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
输出

#!/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


你可以看到源代码。

什么是
pythonpath
?@Aran Fey因为它是一个变量,我相信它一定是他一条Python命令的路径。确切地说,它是一条Python可执行文件的路径。与此同时,我在这里找到了一个解决方法:。因此,命令是
/path/to/python/path/to/check\u python\u script.py
。使用此命令,check_输出按预期工作。它不适用于我,与我自己的初始脚本的问题相同:输出的结果=process.communicate()[0]。打印了拆分行(),但输出的变量为空列表。也许它在Python3上工作,我在2.7.15上工作。