我正在分发一个内部 python 库,如果用户在运行此文件时使用 anaconda,我会自动更新库的依赖项。 (这是应要求的。如果由我决定,我会让用户控制他们自己的包。)
到目前为止,我想出了类似的东西
def _user_has_conda():
cmd = 'conda --help'
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if len(out) > 0 and len(err) == 0:
return True
else:
return False
但这实际上只告诉我用户是否在他们的系统上安装了 anaconda,而不是当前 python 进程是否在 anaconda 环境中运行。
我注意到,当我启动 python 或 ipython shell 时,我看到“Python 3.3.3 |Continuum Analytics, Inc|”在顶部。我的下一个想法是尝试找到如何获取此字符串以查看“Continuum Analytics”是否存在,如果存在,则假设用户正在 anaconda 中运行。
我确信还有更好的想法,这就是我来这里的原因。
谢谢。
最佳答案
我来自 Continuum,所以让我快速记下:您将获得不同的 sys.version
字符串,具体取决于您是否使用 conda
安装Anaconda Python Distribution 或简称为 Python。所以从 conda create -n full_apd anaconda
你会得到一个 sys.version
字符串,如下所示:
$ python -c "import sys; print sys.version"
2.7.6 |Anaconda 1.8.0 (x86_64)| (default, Jan 10 2014, 11:23:15)
[GCC 4.0.1 (Apple Inc. build 5493)]
如果您使用 miniconda
或在刚刚指定了 python
的 conda
环境中工作(例如 conda create -n base_py27 python=2.7
):
$ python -c "import sys; print sys.version"
2.7.6 |Continuum Analytics, Inc.| (default, Jan 10 2014, 11:23:15)
[GCC 4.0.1 (Apple Inc. build 5493)]
如果您只是直接下载并安装了完整的 Anaconda Python 发行版,您将获得前者:
$ python -c "import sys; print sys.version"
2.7.6 |Anaconda 1.8.0 (x86_64)| (default, Jan 10 2014, 11:23:15)
[GCC 4.0.1 (Apple Inc. build 5493)]
关于python - 任何方式来判断用户的 python 环境是否是 anaconda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21282363/