我安装了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。)
我也遇到过类似的问题,我找到了一个适用于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内核。您需要手动为每个环境添加内核,方法如下:
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
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-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屏幕截图: