如何将 R conda env 连接到 jupyter notebook [英] How to connect R conda env to jupyter notebook
问题描述
我正在使用以下代码创建 conda 环境
I am creating conda environment using following code
conda create --prefix r_venv_conda r=3.3 r-essentials r-base --y
然后我通过跟随
conda activate r_venv_conda/
然后我尝试运行 Jupyter Notebook(通过运行 jupyter notebook
来运行 jupyter 希望能连接 R env.但是,我收到以下错误
Then I tried to run Jupyter Notebook (by running jupyter notebook
to run jupyter hoping that will connect R env. However, I am getting following error
Traceback (most recent call last):
File "/home/Documents/project/r_venv_conda/bin/jupyter-notebook", line 7, in <module>
from notebook.notebookapp import main
File "/home/Documents/project/r_venv_conda/lib/python3.6/site-packages/notebook/__init__.py", line 25, in <module>
from .nbextensions import install_nbextension
File "/home/Documents/project/r_venv_conda/lib/python3.6/site-packages/notebook/nbextensions.py", line 26, in <module>
from .config_manager import BaseJSONConfigManager
File "/home/Documents/project/r_venv_conda/lib/python3.6/site-packages/notebook/config_manager.py", line 14, in <module>
from traitlets.config import LoggingConfigurable
File "/home/Documents/project/r_venv_conda/lib/python3.6/site-packages/traitlets/config/__init__.py", line 6, in <module>
from .application import *
File "/home/Documents/project/r_venv_conda/lib/python3.6/site-packages/traitlets/config/application.py", line 38, in <module>
import api.helper.background.config_related
ModuleNotFoundError: No module named 'api'
我该如何解决这个问题?
How can I fix this issue?
推荐答案
Jupyter 不会自动识别 Conda 环境,无论是否激活.
Jupyter does not automatically recognize Conda environments, activated or not.
首先,对于作为内核运行的环境,它需要安装适当的内核包.在这种情况下,就是r-irkernel
,所以你需要运行
First, for an environment to run as a kernel, it needs to have the appropriate kernel package installed. In this case, that is r-irkernel
, so you need to run
conda install -n r_venv_conda r-irkernel
对于 Python 内核,它是 ipykernel
.
For Python kernels, it's ipykernel
.
其次,内核需要注册到 Jupyter.如果您通过 Conda 安装了 Jupyter(比如在 Anaconda base 环境中),那么我建议使用 nb_conda_kernels
包,它可以自动发现内核就绪的 Conda-envs.这必须安装在安装了 jupyter
的环境中(你只需要一个!),例如,如果这是 base,那么
Second, kernels need to be registered with Jupyter. If you have Jupyter installed via Conda (say in an Anaconda base env), then I recommend using the nb_conda_kernels
package, which enables auto-discovery of kernel-ready Conda-envs. This must be installed in the env that has jupyter
installed (you only need one!), for example, if this is base, then
conda install -n base nb_conda_kernels
请阅读文档了解详情.
如果您使用的是系统级安装的 Jupyter(即不是由 Conda 安装的),那么您需要手动注册您的内核.
If you are using a system-level installation of Jupyter (i.e., not installed by Conda), then you need to manually register your kernel.
conda activate r_venv_conda
Rscript -e 'IRkernel::installspec(name="ir33", displayname="R 3.3")'
您可以在其中为 name
和 displayname
设置自己的值.查看 IRkernel 了解详情.
where you can set your own values for name
and displayname
. See IRkernel for details.
如果使用安装了 Conda 的 Jupyter,它只需要安装在单个环境中.这是在运行 jupyter notebook
之前应该激活的环境.运行后,您可以选择要使用的内核.
If using a Conda-installed Jupyter, again, it only needs to be installed in a single env. This is the env that should be activated before running jupyter notebook
. Once running, then you select the kernel you wish to use.
这篇关于如何将 R conda env 连接到 jupyter notebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!