如何将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'
如何解决此问题?
推荐答案
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 env中安装),那么我建议使用
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屋!