Python 将 Conda 环境与 Jupyter Notebook 链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37433363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:24:02  来源:igfitidea点击:

Link Conda environment with Jupyter Notebook

pythonipythonjupyterjupyter-notebookminiconda

提问by Thomas Dehaeze

I'm trying to set a good environnement for doing some scientific stuff with python. To do so, I installed Jupyter & miniconda.

我正在尝试为使用 python 做一些科学工作设置一个良好的环境。为此,我安装了 Jupyter 和 miniconda。

Then I want to be able to have different environnement and use them with Jupyter notebooks. So I created two custom envs with conda : py27 and py35.

然后我希望能够拥有不同的环境并将它们与 Jupyter 笔记本一起使用。所以我用 conda 创建了两个自定义环境:py27 和 py35。

> conda env list
# conda environments:
#
py27                     /Users/***/miniconda3/envs/py27
py35                     /Users/***/miniconda3/envs/py35
root                  *  /Users/***/miniconda3

Then on my notebook I have two kernels python 2and python 3. Inside a notebook, I get the following with the python3 kernel :

然后在我的笔记本上,我有两个内核python 2python 3. 在笔记本中,我使用 python3 内核得到以下信息:

> import sys
> print(sys.executable)
/Users/***/miniconda3/envs/py35/bin/python

And this with the python2 kernel :

这与 python2 内核:

> import sys
> print(sys.executable)
/usr/local/opt/python/bin/python2.7
  • How can I set the sys.executableto miniconda env for python2 ?
  • How can I bind a conda env with a notebook kernel ?
  • Is doing source activate py35has a link with jupyter notebook?
  • 如何将sys.executablepython2设置为 miniconda env ?
  • 如何将 conda env 与笔记本内核绑定?
  • 是做source activate py35有联系jupyter notebook吗?

I think I really missed something.

我想我真的错过了一些东西。

Thank you everyone.

谢谢大家。

--- edit

- - 编辑

I have multiple jupyter bin :

我有多个 jupyter bin :

> where jupyter
/usr/local/bin/jupyter
/usr/local/bin/jupyter
/Users/ThomasDehaeze/miniconda3/bin/jupyter

I have only one kernel here /usr/local/share/jupyter/kernels/python2. But inside Jupyter, I have two kernels, python2and python3. Where can I find the other one ?

我这里只有一个内核/usr/local/share/jupyter/kernels/python2。但是在 Jupyter 内部,我有两个内核,python2以及python3. 我在哪里可以找到另一个?



I modified kernel.jsonfrom /usr/local/share/jupyter/kernels/python2:

我修改kernel.json/usr/local/share/jupyter/kernels/python2

{
 "display_name": "Python 2",
 "language": "python",
 "argv": [
  "/Users/***/miniconda3/envs/py27/bin/python2.7",
  "-m",
  "ipykernel",
  "-f",
  "{connection_file}"
 ]
}

And then :

进而 :

import sys
print(sys.executable)
/usr/local/opt/python/bin/python2.7

So nothing has changed

所以什么都没有改变

回答by 5agado

For Anaconda I suggest you a much easier and proper solution; just give a look at the nb_conda_kernels package.

对于 Anaconda,我建议您使用一个更简单、更合适的解决方案;看看nb_conda_kernels 包

It allows you to "manage your conda environment-based kernels inside the Jupyter Notebook".

它允许您“在 Jupyter Notebook 中管理基于 conda 环境的内核”。

Is should be included since Anaconda version 4.1.0, otherwise simply use

自 Anaconda 版本 4.1.0 起应包括在内,否则只需使用

conda install nb_conda

Now you should be able to manage all direcly from the Notebook interface.

现在您应该能够从 Notebook 界面直接管理所有内容。

回答by Nihal Sangeeth

Assuming your conda-env is named cenv, it is as simple as :

假设您的 conda-env 名为 named cenv,它很简单:

    $ conda activate cenv
    (cenv)$ conda install ipykernel
    (cenv)$ ipython kernel install --user --name=<any_name_for_kernel>
    (cenv($ conda deactivate

If you restart your jupyter notebook/lab you will be able to see the new kernel available.

如果您重新启动 jupyter notebook/lab,您将能够看到可用的新内核。

PS: If you are using virtualenv etc. the above steps hold good.

PS:如果您使用的是 virtualenv 等,上述步骤很有效。

回答by vedrano

Not sure what else did help, but for me crucial was to install nb_conda_kernelsin root conda environment. Attempting to install it in specific conda environment did not end up in having Jupyter Notebook be able to use other conda environment other than default one.

不确定还有什么帮助,但对我来说至关重要的是安装nb_conda_kernels在 root conda 环境中。尝试在特定的 conda 环境中安装它并没有让 Jupyter Notebook 能够使用默认环境以外的其他 conda 环境。

conda install -n root nb_conda_kernels

jupyter notebook

回答by Thomas Dehaeze

I found the solution. The setup for the kernels where located here ~/Library/Jupyter/kernels/.

我找到了解决方案。位于此处的内核设置~/Library/Jupyter/kernels/

Then I modified the kernel.jsonfile and set the right path to python.

然后我修改了kernel.json文件并将正确的路径设置为python。

Now it's working.

现在它正在工作。