【发布时间】:2018-02-12 01:07:42
【问题描述】:
我一直在使用多个 conda 环境(内核)运行 Jupyter 笔记本。这是通过安装nb_conda 和nb_conda_kernels 完成的。
安装 both 上述软件包并重新启动笔记本服务器后,我似乎可以访问 jupyter 笔记本中的两个 conda 环境。但是,我无法确认底层 shell 是否具有正确的环境。例如,如果我启动两台笔记本服务器,一台使用 Python 2.7,另一台使用 3.6,我会得到 Python 版本的预期答案,但不会得到执行的 shell 命令。
Python 2.7.13:
import sys
print(sys.version)
#succeed evidence for running py < 3
import commands
commands.getoutput('which python')
输出:
2.7.13 |Anaconda custom (64-bit)| (default, Dec 20 2016, 23:09:15)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
'/path/python/anaconda3/envs/py36/bin/python'
在这种情况下,我预计 which python 会生成在 Python 2.7.13 环境中处于活动状态的 python 版本。但我知道返回的路径实际上是 Python 3.6 环境中使用的python(见下文)
Python 3.6:
import sys
print(sys.version)
import subprocess
subprocess.check_output(["which","python"])
输出:
3.6.1 |Anaconda 4.4.0 (64-bit)| (default, May 11 2017, 13:09:58)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
b'/path/python/anaconda3/envs/py36/bin/python\n'
另外,在 Python 3.6 环境中我遇到了这个失败,这是有道理的,因为 commands 模块在 Python 3 中被淘汰了:
# fail (evidence for running py 3.6 env)
import commands
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-9cbf0185e88f> in <module>()
1 # fail (evidence for running py 3.6 env)
----> 2 import commands
ModuleNotFoundError: No module named 'commands'
因此,无论哪种情况,which python 的输出都给出了我启动 Jupyter notebook 的环境中的 python 版本。这让我相信,虽然 Python 环境符合预期,但 shell 环境在某种程度上与 Python conda 环境不一致。为什么这是真的?会不会造成问题?
【问题讨论】:
标签: python jupyter-notebook conda