Jupyter Notebook中未显示Conda环境

本文翻译自:Conda environments not showing up in Jupyter Notebook

I installed Anaconda (with Python 2.7), and installed Tensorflow in an environment called tensorflow . 我安装了Anaconda(使用Python 2.7),并在名为tensorflow的环境中安装了tensorflow I can import Tensorflow successfully in that environment. 我可以在该环境中成功导入Tensorflow。

The problem is that Jupyter Notebook does not recognize the new environment I just created. 问题在于Jupyter Notebook无法识别我刚刚创建的新环境。 No matter I start Jupyter Notebook from the GUI Navigator or from the command line within the tensorflow env, there is only one kernel in the menu called Python [Root] , and Tensorflow cannot be imported. 无论我从GUI导航还是从内部的命令行启动Jupyter笔记本tensorflow ENV,只有一个名为菜单内核Python [Root] ,并且Tensorflow无法导入。 Of course, I clicked on that option multiple times, saved file, re-opened, but these did not help. 当然,我多次单击该选项,保存了文件,然后重新打开,但是这些无济于事。

Strangely, I can see the two environments when I open the Conda tab on the front page of Jupyter. 奇怪的是,当我打开Jupyter主页上的Conda选项卡时,可以看到两种环境。 But when I open the Files tab, and try to new a notebook, I still end up with only one kernel. 但是,当我打开Files选项卡,并尝试new笔记本,我还是结束了只有一个内核。

I looked at this question: Link Conda environment with Jupyter Notebook But there isn't such a directory as ~/Library/Jupyter/kernels on my computer! 我看着这个问题: 用Jupyter Notebook链接Conda环境但是我的计算机上没有~/Library/Jupyter/kernels这样的目录! This Jupyter directory only has one sub-directory called runtime . 这个Jupyter目录只有一个子目录,称为runtime

I am really confused. 我真的很困惑。 Are Conda environments supposed to become kernels automatically? Conda环境是否应该自动成为内核? (I followed https://ipython.readthedocs.io/en/stable/install/kernel_install.html to manually set up the kernels, but was told that ipykernel was not found.) (我按照https://ipython.readthedocs.io/en/stable/install/kernel_install.html手动设置了内核,但被告知未找到ipykernel 。)


#1楼

参考:https://stackoom.com/question/2gAsZ/Jupyter-Notebook中未显示Conda环境


#2楼

The annoying thing is that in your tensorflow environment, you can run jupyter notebook without installing jupyter in that environment . 令人讨厌的是,在您的张量tensorflow环境中,您可以运行jupyter notebook 而无需在该环境中安装jupyter Just run 赶紧跑

(tensorflow) $ conda install jupyter

and the tensorflow environment should now be visible in Jupyter Notebooks started in any of your conda environments as something like Python [conda env:tensorflow] . tensorflow环境现在应该是可见的Jupyter笔记本电脑在你的任何启动conda环境,因为类似Python [conda env:tensorflow]


#3楼

If your environments are not showing up, you probably do not have nb_conda_kernels installed in the environment where Jupyter is installed. 如果您的环境未显示,则可能是在安装nb_conda_kernels的环境中未安装nb_conda_kernels。 Anaconda's documentation states that Anaconda的文档指出

nb_conda_kernels should be installed in the environment from which you run Jupyter Notebook or JupyterLab. nb_conda_kernels应该安装在运行Jupyter Notebook或JupyterLab的环境中。 This might be your base conda environment, but it need not be. 这可能是您的基本conda环境,但不一定如此。 For instance, if the environment notebook_env contains the notebook package, then you would run 例如,如果环境notebook_env包含notebook软件包,则可以运行

 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; 例如,要访问Python环境,它必须具有ipykernel软件包。 eg 例如

 conda install -n python_env ipykernel 

To utilize an R environment, it must have the r-irkernel package; 要使用R环境,它必须具有r-irkernel软件包。 eg 例如

 conda install -n r_env r-irkernel 

For other languages, their corresponding kernels must be installed. 对于其他语言,必须安装其相应的内核

Note that at the time originally posting this, there was a possible cause from nb_conda not yet supporting Python 3.6 environments . 请注意,在最初发布此内容时,可能是由于nb_conda不支持Python 3.6环境引起的

If other solutions fail to get Jupyter to recognize other conda environments, you can always install and run jupyter from within a specific environment. 如果其他解决方案无法使Jupyter识别其他conda环境,则始终可以在特定环境中安装并运行jupyter You may not be able to see or switch to other environments from within Jupyter though. 但是,您可能无法从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

Notice that I am running Python 3.6.1 in this notebook: 请注意,我正在此笔记本中运行Python 3.6.1: 在此处输入图片说明

Note that if you do this with many environments, the added storage space from installing Jupyter into every environment may be undesirable (depending on your system). 请注意,如果您在许多环境中执行此操作,则可能不希望将Jupyter安装到每个环境中而增加存储空间(取决于您的系统)。


#4楼

Just run conda install ipykernel in your new environment, only then you will get a kernel with this env. 只要在您的新环境中运行conda install ipykernel ,就可以使用此env获得内核。 This works even if you have different versions installed in each envs and it doesn't install jupyter notebook again. 即使您在每个环境中安装了不同的版本,也不会再次安装jupyter Notebook,此方法仍然有效。 You can start youe notebook from any env you will be able to see newly added kernels. 您可以从任何环境中启动笔记本,您将能够看到新添加的内核。


#5楼

I ran into this same problem where my new conda environment, myenv , couldn't be selected as a kernel or a new notebook. 我遇到了同样的问题,我的新conda环境myenv无法选择为内核或新笔记本。 And running jupter notebook from within the env gave the same result. 从环境中运行jupter notebook可以得到相同的结果。

My solution, and what I learned about how Jupyter notebooks recognizes conda-envs and kernels: 我的解决方案以及有关Jupyter笔记本如何识别conda-envs和内核的知识:

Installing jupyter and ipython to myenv with conda: 使用conda将jupyter和ipython安装到myenv

conda install -n myenv ipython jupyter

After that, running jupter notebook outside any env listed myenv as a kernel along with my previous environments. 之后,在任何环境之外运行jupter notebookmyenv与以前的环境一起列为内核。

Python [conda env:old]
Python [conda env:myenv]

Running the notebook once I activated the environment: 激活环境后运行笔记本:

source activate myenv
jupyter notebook

hides all my other environment-kernels and only shows my language kernels: 隐藏我所有其他环境内核,只显示我的语言内核:

python 2
python 3
R

#6楼

I don't think the other answers are working any more, as conda stopped automatically setting environments up as jupyter kernels. 我认为其他答案不再有效,因为conda停止自动将环境设置为jupyter内核。 You need to manually add kernels for each environment in the following way: 您需要通过以下方式为每个环境手动添加内核:

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

As documented here: http://ipython.readthedocs.io/en/stable/install/kernel_install.html#kernels-for-different-environments Also see this issue . 如此处所述: http : //ipython.readthedocs.io/zh-CN/stable/install/kernel_install.html#kernels-for-different-environments也请参阅此问题

Addendum: You should be able to install the nb_conda_kernels package with conda install nb_conda_kernels to add all environments automatically, see https://github.com/Anaconda-Platform/nb_conda_kernels 附录:您应该能够通过nb_conda_kernels conda install nb_conda_kernels安装nb_conda_kernels软件包,以自动添加所有环境,请参见https://github.com/Anaconda-Platform/nb_conda_kernels

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值