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


当前回答

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

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

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

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

我们在这个问题上做了很多努力,以下是对我们有效的方法。如果你使用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屏幕截图:

这适用于我在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”周围包含引号)