简体   繁体   English

在不同的 conda 环境中将 R kernel 安装到 jupyter notebook

[英]installing R kernel to jupyter notebook in a different conda environment

I have a conda environment containing all packages for jupyter notebook (say it's called jupyter_env . In a different conda environment I have R installed including r-irkernel (say the env is called R_env ).我有一个 conda 环境,其中包含 jupyter notebook 的所有软件包(比如说它叫做jupyter_env 。在不同的 conda 环境中,我安装了 R ,包括r-irkernel (比如说 env 叫做R_env )。

For python kernels I can easily make a python kernel in a specific environment (called eg pyth27 ) available to my jupyter installation in a different environment:对于 python 内核,我可以轻松地使 python kernel 在特定环境(例如pyth27 )中可用于我在不同环境中的 jupyter 安装:

(pyth27) > python -m ipykernel install --prefix=/path/to/jupyter/env --name "python27"

Is there anything similar possible for the R kernel? R kernel 是否有类似的可能? So far I can only run the R kernel using a jupyter installation within the same environment( R_env ).到目前为止,我只能在同一环境( R_env )中使用 jupyter 安装运行 R kernel 。

One solution might be the nb-conda_kernels package.一种解决方案可能是nb-conda_kernels package。 However there I'm not clear if it always adds all available kernels from all environments or whether I can specify which environments should be searched.但是,我不清楚它是否总是添加来自所有环境的所有可用内核,或者我是否可以指定应该搜索哪些环境。

My question is similar to this one https://github.com/jupyter/jupyter/issues/397 .我的问题类似于这个https://github.com/jupyter/jupyter/issues/397 Only that I don't want to use the base environment to start jupyter but a dedicated environment.只是我不想使用基础环境来启动 jupyter,而是使用专用环境。

As described on https://github.com/IRkernel/IRkernel , the r-ikernel package provides a mechanism similar to python -m ipykernel install , to be run in R: As described on https://github.com/IRkernel/IRkernel , the r-ikernel package provides a mechanism similar to python -m ipykernel install , to be run in R:

R > IRkernel::installspec() 

To run this from Bash, you can do要从 Bash 运行它,你可以这样做

(R_env) > Rscript -e "IRkernel::installspec()"

Now the tricky part , due to Jupyter and R being in different environments: According to https://github.com/IRkernel/IRkernel/issues/499 , IRkernel::installspec() requires the jupyter-kernelspec command.现在棘手的部分,由于 Jupyter 和 R 处于不同的环境中:根据https://github.com/IRkernel/IRkernel/issues/499IRkernel::installspec()需要jupyter-kernelspec命令。 I've tested two methods to provide it (to be done before issuing the above commands):我已经测试了两种方法来提供它(在发出上述命令之前完成):

  • jupyter-kernelspec is part of Jupyter and hence in the file tree of jupyter_env , so add its path to PATH jupyter-kernelspec是 Jupyter 的一部分,因此在jupyter_env的文件树中,因此将其路径添加到PATH

     (R_env) > export PATH="</path/to/conda>/envs/jupyter_env/bin:$PATH"
  • jupyter-kernelspec is included in the jupyter_client conda package, so you can do jupyter-kernelspec包含在jupyter_client package 中,所以你可以做

    (R_env) > conda install jupyter_client

    Caveat: this installs a number of dependencies, including Python.警告:这会安装许多依赖项,包括 Python。

I opted for the first method to keep R_env free of Python packages.我选择了第一种方法来保持R_env没有 Python 包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM