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

2024-04-19 13:58:31 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

command = ' '.join([pythonpath, '-V'])
output = subprocess.check_output( command, shell=True )
print output

但在这种情况下,check\u output不能按预期工作:输出显示在终端中,但不进入变量output。你知道吗


Tags: topath版本脚本可执行文件output源代码check
1条回答
网友
1楼 · 发布于 2024-04-19 13:58:31

代码

#!/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。你知道吗

相关问题 更多 >