如何从 Python 脚本内部获取当前的 Python 解释器路径? [英] How to get the current Python interpreter path from inside a Python script?

查看:29
本文介绍了如何从 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆