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


恼人的是,在你的tensorflow环境中,你可以运行jupyter notebook,而无需在该环境中安装jupyter。你就跑

(tensorflow) $ conda install jupyter

tensorflow环境现在应该在Jupyter notebook中可见,在任何conda环境中启动,类似于Python [conda env:tensorflow]。

如果您的环境没有显示,请确保您已经安装

nb_conda_kernels在Jupyter环境中 你想要访问的Python环境中的ipykernel和ipywidgets(注意,ipywidgets是为了启用一些Juptyer功能,而不是环境可见性,请参阅相关文档)。

Anaconda的文档表明

nb_conda_kernels should be installed in the environment from which you run Jupyter Notebook or JupyterLab. This might be your base conda environment, but it need not be. For instance, if the environment notebook_env contains the notebook package, then you would run conda install -n notebook_env nb_conda_kernels Any other environments you wish to access in your notebooks must have an appropriate kernel package installed. For instance, to access a Python environment, it must have the ipykernel package; e.g. conda install -n python_env ipykernel To utilize an R environment, it must have the r-irkernel package; e.g. conda install -n r_env r-irkernel For other languages, their corresponding kernels must be installed.

除了Python,通过安装适当的*内核包,Jupyter可以访问大量其他语言的内核,包括R、Julia、Scala/Spark、JavaScript、bash、Octave,甚至MATLAB。


请注意,在最初发布这篇文章的时候,可能是nb_conda还不支持Python 3.6环境造成的。

如果其他解决方案无法让Jupyter识别其他conda环境,则始终可以在特定环境中安装和运行Jupyter。不过,你可能无法从木星内部看到或切换到其他环境。

$ conda create -n py36_test -y python=3.6 jupyter
$ source activate py36_test
(py36_test) $ which jupyter
/home/schowell/anaconda3/envs/py36_test/bin/jupyter
(py36_test) $ jupyter notebook

注意,我在这个笔记本中运行的是Python 3.6.1:

注意,如果在许多环境中都这样做,那么在每个环境中安装Jupyter所增加的存储空间可能是不可取的(取决于您的系统)。

只要在你的新环境中运行conda install ipykernel,只有这样你才能得到一个带有这个env的内核。即使你在每个envs中安装了不同的版本,它也不会再次安装jupyter笔记本。你可以从任何环境开始你的笔记本,你将能够看到新添加的内核。

我也遇到了同样的问题,我的新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

我不认为其他答案是工作了,因为conda停止自动设置环境作为jupyter内核。您需要手动为每个环境添加内核,方法如下:

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

如下所示:http://ipython.readthedocs.io/en/stable/install/kernel_install.html#kernels-for-different-environments 请参见本期。

附录: 您应该能够使用conda install nb_conda_kernels安装nb_conda_kernels包来自动添加所有环境,请参阅https://github.com/Anaconda-Platform/nb_conda_kernels

我们在这个问题上做了很多努力,以下是对我们有效的方法。如果你使用conda-forge通道,确保你使用的是从conda-forge更新的包是很重要的,即使是在你的Miniconda根环境中。

所以安装Miniconda,然后做:

conda config --add channels conda-forge --force
conda update --all  -y
conda install nb_conda_kernels -y
conda env create -f custom_env.yml -q --force
jupyter notebook

你的自定义环境将作为可用的内核显示在Jupyter中,只要你的custom_env中列出了ipykernel以供安装。Yml文件,就像这个例子:

name: bqplot
channels:
- conda-forge
- defaults
dependencies:
- python>=3.6
- bqplot
- ipykernel

为了证明它适用于许多自定义环境,这里有一个Windows屏幕截图:

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

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

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 install nb_conda_kernels

(在运行jupyter notebook的conda环境中)将使所有conda envs自动可用。要访问其他环境,必须安装相应的内核。这是裁判。

虽然@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

总结(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',所以并不是每个人都遇到了这个问题。

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

在我的例子中,使用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

nb_conda_kernels包是在conda中使用jupyter的最佳方式。通过最小的依赖关系和配置,它允许您使用运行在不同环境中的jupyter笔记本上的其他conda环境。引用其文件:

Installation This package is designed to be managed solely using conda. It should be installed in the environment from which you run Jupyter Notebook or JupyterLab. This might be your base conda environment, but it need not be. For instance, if the environment notebook_env contains the notebook package, then you would run conda install -n notebook_env nb_conda_kernels Any other environments you wish to access in your notebooks must have an appropriate kernel package installed. For instance, to access a Python environment, it must have the ipykernel package; e.g. conda install -n python_env ipykernel To utilize an R environment, it must have the r-irkernel package; e.g. conda install -n r_env r-irkernel For other languages, their corresponding kernels must be installed.

然后你需要做的就是启动jupyter笔记本服务器:

conda activate notebook_env  # only needed if you are not using the base environment for the server
# conda install jupyter # in case you have not installed it already
jupyter


尽管有太多的答案,@merv也在努力改进,但仍然很难找到一个好的答案。我做了这个CW,所以请投票给它的顶部或改进它!

这适用于我在windows 10和最新解决方案:

1)进入conda环境(激活your_env_name)

2) conda install -n your_env_name ipykernel

3) python -m ipykernel install——user——name build_central——display name" your_env_name"

(注意:在步骤3中,“your_env_name”周围包含引号)

可能的特定渠道问题

我有这个问题(再次),原来我从conda-forge频道安装;将其移除并从蟒蛇通道重新安装,而不是为我修复它。

更新:我在一个新的env中再次遇到了同样的问题,这次我确实从anaconda通道安装了nb_conda_kernels,但我的jupyter_client来自conda forge通道。卸载nb_conda_kernels并重新安装会将其更新到更高优先级的通道。

所以请确保你从正确的渠道安装:)

我也遇到过类似的问题,我找到了一个适用于Mac、Windows和Linux的解决方案。它需要上面答案中的几个关键成分:

为了能够看到conda env在Jupyter笔记本,你需要:

the following package in you base env: conda install nb_conda the following package in each env you create: conda install ipykernel check the configurationn of jupyter_notebook_config.py first check if you have a jupyter_notebook_config.py in one of the location given by jupyter --paths if it doesn't exist, create it by running jupyter notebook --generate-config add or be sure you have the following: c.NotebookApp.kernel_spec_manager_class='nb_conda_kernels.manager.CondaKernelSpecManager'

您可以在终端看到的环境:

在Jupyter实验室,你可以看到相同的env上面的笔记本和控制台:

当你打开笔记本时,你可以选择你的环境:

安全的方法是创建一个特定的env,从中运行envjupyter lab命令的示例。激活你的环境。然后添加jupyter实验室扩展示例jupyter实验室扩展。然后你就可以运行木星实验室了

添加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上测试

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

pip install ipykernel

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

python -m ipykernel install --name = my_env

仅使用环境变量:

python -m ipykernel install --user --name $(basename $VIRTUAL_ENV)

我在使用vscode服务器时遇到了这个问题。 在名为“base”的conda环境中,我安装了1.2.0版本的opennmt-py,但我想在conda环境“opennmt2”中运行jupyter notebook,其中包含使用opennmt-py 2.0的代码。 我通过在conda(opennmt2)中重新安装jupyter解决了这个问题。

conda install jupyter

重新安装后,在opennmt2环境中执行jupyter notebook将执行新安装的jupyter

where jupyter 
/root/miniconda3/envs/opennmt2/bin/jupyter
/root/miniconda3/bin/jupyter

这是一个旧线程,但是在Anaconda提示符中运行它,在我感兴趣的环境中,对我来说是有效的:

ipython kernel install --name "myenvname" --user

我只是想补充之前的答案:如果安装nb_conda_kernels, ipywidgets和ipekernel不能工作,请确保您的Jupyter版本是最新的。我的envs在一切正常工作一段时间后突然停止显示,并在我简单地通过anaconda navigator更新jupyter后恢复工作。

对于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”。我得到的是: