我安装了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。)


当前回答

我不得不运行前3个答案中提到的所有命令来让它工作:

conda install jupyter
conda install nb_conda
conda install ipykernel
python -m ipykernel install --user --name mykernel

其他回答

对于conda 4.5.12,适用于我的是(我的虚拟环境被称为nwt)

conda create --name nwt python=3

之后,我需要激活虚拟环境并安装ipykernel

activate nwt
pip install ipykernel

那么对我有效的方法是:

python -m ipykernel install --user --name env_name --display-name "name of your choosing."

例如,我使用'nwt'作为虚拟env的显示名称。在运行上面的命令之后。再次在Anaconda Prompt中运行“jupyter notebook”。我得到的是:

总结(tldr)

如果你想让'python3'内核总是在它启动的环境中运行Python安装,删除User 'python3'内核,它优先于当前环境:

jupyter kernelspec remove python3

完整的解决方案

我将针对以下情况发布一个替代和更简单的解决方案:

您已经创建了一个conda环境 这个环境已经安装了jupyter(它也安装了ipykernel) 当您运行命令jupyter notebook并通过单击“new”下拉菜单中的“python3”创建一个新notebook时,该notebook将从基本环境而不是当前环境执行python。 您可能希望在任何环境中启动带有“python3”的新笔记本时,都会从该环境执行Python版本,而不是从基础环境执行

在解决方案的其余部分中,我将使用“test_env”作为环境的名称。另外,注意'python3'是内核的名称。

目前票数最高的答案确实有效,但还有另一种选择。它说要做以下事情:

python -m ipykernel install --user --name test_env --display-name "Python (test_env)"

这将为您提供使用test_env环境的选项,而不管您从哪个环境启动jupyter notebook。但是,使用'python3'启动笔记本电脑仍将使用基本环境中的Python安装。

可能发生的情况是存在一个用户python3内核。运行命令jupyter kernelspec list列出所有环境。例如,如果你有一个mac,你将返回以下(我的用户名是Ted)。

python3       /Users/Ted/Library/Jupyter/kernels/python3

木星在这里所做的是在三条不同的路径上寻找内核。它从用户,到环境,到系统。有关它搜索每个操作系统的路径的详细信息,请参阅本文档。

上面的两个内核都在User路径中,这意味着无论您从哪个环境启动jupyter笔记本,它们都是可用的。这也意味着如果在环境级别上存在另一个“python3”内核,那么您将永远无法访问它。

对我来说,从你启动笔记本电脑的环境中选择“python3”内核应该在该环境中执行Python更有意义。

你可以通过在操作系统的Env搜索路径中查看是否有另一个'python3'环境(参见上面文档的链接)。对于我来说(在我的mac上),我发出了以下命令:

 ls /Users/Ted/anaconda3/envs/test_env/share/jupyter/kernels

我确实在那里列出了一个'python3'内核。

多亏了这个GitHub问题评论(看看第一个响应),你可以用以下命令删除用户'python3'环境:

jupyter kernelspec remove python3

现在当你运行jupyter kernelspec list时,假设test_env仍然是活动的,你会得到以下结果:

python3       /Users/Ted/anaconda3/envs/test_env/share/jupyter/kernels/python3

注意,这个路径位于test_env目录中。如果您创建一个新环境,安装jupyter,激活它,并列出内核,您将得到另一个位于其环境路径下的'python3'内核。

User 'python3'内核优先于任何Env 'python3'内核。通过移除它,活动环境“python3”内核就被公开了,并且每次都可以被选择。这消除了手动创建内核的需要。在软件开发方面,它也更有意义,因为人们希望将自己隔离到一个单一的环境中。运行与宿主环境不同的内核似乎不太自然。

默认情况下,似乎并不是每个人都安装了这个User 'python3',所以并不是每个人都遇到了这个问题。

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环境到Jupyter:

在Anaconda提示中:

执行conda activate <env name> 执行conda install -c anaconda ipykernel命令 执行python -m ipykernel install——user——name=<env name> **在conda 4.8.3 4.11.0上测试

我不得不运行前3个答案中提到的所有命令来让它工作:

conda install jupyter
conda install nb_conda
conda install ipykernel
python -m ipykernel install --user --name mykernel