如何从Python脚本中获取当前的Python解释器path?

我想从一个Python脚本运行一个Python脚本和subprocess ,我希望使用相同的解释器为他们每个人做。

我使用的是virtualenv,所以我想要做一些事情:

 subprocess.Popen('%s script.py' % python_bin) 

我如何得到python_bin

它应该是virtualenv之外的/usr/bin/python ,以及virtualenv中的/path/to/env/bin/python

解释器的名称存储在variablessys.executable中

我发现它:

 >>> import sys >>> help(sys) ... DATA __stderr__ = <open file '<stderr>', mode 'w' at 0x110029140> __stdin__ = <open file '<stdin>', mode 'r' at 0x110029030> __stdout__ = <open file '<stdout>', mode 'w' at 0x1100290b8> api_version = 1013 argv = [''] builtin_module_names = ('__builtin__', '__main__', '_ast', '_codecs', ... byteorder = 'big' copyright = 'Copyright (c) 2001-2009 Python Software Foundati...ematis... dont_write_bytecode = False exc_value = TypeError('arg is a built-in module',) exec_prefix = '/usr/bin/../../opt/freeware' executable = '/usr/bin/python_64' 

只是为了确保:

 >>> import sys >>> sys.executable '/usr/bin/python' >>>