如何从Python脚本中获取当前的Python解释器路径? [英] How to get the current Python interpreter path from inside a Python script?
本文介绍了如何从Python脚本中获取当前的Python解释器路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用subprocess
从Python脚本运行Python脚本,并且希望对每个脚本使用相同的解释器.
I want to run a Python script from a Python script with subprocess
, and I wish to do it using the same interpreter for each of them.
我正在使用virtualenv,所以我想做类似的事情:
I'm using virtualenv, so I'd like to do something like:
subprocess.Popen('%s script.py' % python_bin)
如何获取python_bin
?
它应该在virtualenv外的/usr/bin/python
中,并且在virtualenv中的/path/to/env/bin/python
中.
It should be /usr/bin/python
outside a virtualenv, and /path/to/env/bin/python
in a virtualenv.
推荐答案
解释器的名称存储在变量 sys.executable
The name of the interpreter is stored in the variable sys.executable
这篇关于如何从Python脚本中获取当前的Python解释器路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文