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


当前回答

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

其他回答

我也遇到过类似的问题,我找到了一个适用于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实验室扩展。然后你就可以运行木星实验室了

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

(tensorflow) $ conda install jupyter

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

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

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,所以请投票给它的顶部或改进它!

我不认为其他答案是工作了,因为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