How to get the current Python interpreter path from inside a Python script?
本问题已经有最佳答案,请猛点这里访问。
我想用
我正在使用virtualenv,所以我想做一些类似的事情:
1 | subprocess.Popen('%s script.py' % python_bin) |
我怎样才能得到
它应该在virtualenv之外是
解释器的名称存储在变量sys.executable中。
我发现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | >>> 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' |
只是为了确保:
1 2 3 4 | >>> import sys >>> sys.executable '/usr/bin/python' >>> |