我安装了Anaconda(使用Python 2.7),并在一个名为Tensorflow的环境中安装了Tensorflow。我可以在这个环境中成功导入Tensorflow。

问题是Jupyter Notebook无法识别我刚刚创建的新环境。无论我是从GUI Navigator还是tensorflow env中的命令行启动Jupyter Notebook,菜单中只有一个名为Python [Root]的内核,并且不能导入tensorflow。当然,我多次点击这个选项,保存文件,重新打开,但这些都没有帮助。

奇怪的是,当我打开Jupyter首页上的Conda标签时,我可以看到这两个环境。但是当我打开文件选项卡,并尝试新建一个笔记本时,我仍然只有一个内核。

我看了这个问题: 连接Conda环境与Jupyter Notebook 但是在我的电脑上没有~/Library/Jupyter/kernels这样的目录!这个Jupyter目录只有一个称为runtime的子目录。

我真的很困惑。Conda环境应该自动成为内核吗?(我在https://ipython.readthedocs.io/en/stable/install/kernel_install.html上手动设置了内核,但被告知没有找到ipykernel。)


当前回答

按照iPython文档中的说明,将不同的conda环境添加到Jupyter Notebook中可供选择的内核列表中。总之,安装完ipykernel后,必须在终端中逐个激活conda环境,执行命令python -m ipykernel install——user——name myenv——display-name " python (myenv)",其中myenv就是要添加的环境(内核)。

其他回答

This has been so frustrating, My problem was that within a newly constructed conda python36 environment, jupyter refused to load “seaborn” - even though seaborn was installed within that environment. It seemed to be able to import plenty of other files from the same environment — for example numpy and pandas but just not seaborn. I tried many of the fixes suggested here and on other threads without success. Until I realised that Jupyter was not running kernel python from within that environment but running the system python as kernel. Even though a decent looking kernel and kernel.json were already present in the environment. It was only after reading this part of the ipython documentation: https://ipython.readthedocs.io/en/latest/install/kernel_install.html#kernels-for-different-environments and using these commands:

source activate other-env
python -m ipykernel install --user --name other-env --display-name "Python (other-env)"

我能让一切顺利进行。(我实际上没有使用-user变量)。

我还没有想到的一件事是如何将默认的python设置为“python (other-env)”。目前,从主屏幕打开的现有.ipynb文件将使用系统python。我必须使用内核菜单“更改内核”来选择环境python。

我也遇到了同样的问题,我的新conda环境myenv不能被选为内核或新笔记本。在env中运行jupter notebook也得到了相同的结果。

我的解决方案,以及我了解到Jupyter笔记本如何识别conda-envs和内核:

使用conda将jupyter和ipython安装到myenv:

conda install -n myenv ipython jupyter

在那之后,在任何env之外运行jupter notebook时,将myenv与我以前的环境一起列为内核。

Python [conda env:old]
Python [conda env:myenv]

启动环境后运行笔记本:

source activate myenv
jupyter notebook

隐藏所有其他环境内核,只显示我的语言内核:

python 2
python 3
R

首先你需要激活你的环境。

pip install ipykernel

接下来,您可以通过输入以下命令将虚拟环境添加到Jupyter:

python -m ipykernel install --name = my_env

在我的例子中,使用Windows 10和conda 4.6.11,通过运行这些命令

conda install nb_conda

conda install -c conda-forge nb_conda_kernels

在我使用conda Jupyter笔记本从同一命令行打开Jupyter后,从终端同时有环境活动并没有做这项工作。

显然,解决方案是从Anaconda Navigator打开Jupyter,进入我的环境:打开Anaconda Navigator,在Environments中选择环境,按下所选环境的“播放”按钮,并选择“用Jupyter Notebook打开”。

Anaconda Navigator中的环境从选定的环境中运行Jupyter

虽然@coolscitist的回答对我来说是有效的,但也有一种方法不会让你的内核环境与完整的jupyter包+deps混淆。 它在ipython文档中有描述,(我怀疑)只有在非基础环境中运行笔记本服务器时才有必要。

conda activate name_of_your_kernel_env
conda install ipykernel
python -m ipykernel install --prefix=/home/your_username/.conda/envs/name_of_your_jupyter_server_env --name 'name_of_your_kernel_env'

你可以用

conda activate name_of_your_jupyter_server_env 
jupyter kernelspec list