![](/img/trans.png)
[英]Installing a ipykernel and running jupyter notebook inside a virtual env - not using conda
[英]How to connect R conda env to jupyter notebook
我正在使用以下代碼創建 conda 環境
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。但是,我收到以下錯誤
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 環境,無論是否激活。
首先,對於作為 kernel 運行的環境,需要安裝相應的 kernel package。 在這種情況下,即r-irkernel
,因此您需要運行
conda install -n r_venv_conda r-irkernel
對於 Python 內核,它是ipykernel
。
其次,內核需要向 Jupyter 注冊。 如果您通過 Conda 安裝了 Jupyter(例如在 Anaconda基礎環境中),那么我建議使用nb_conda_kernels
package ,它可以自動發現內核就緒的 Conda-envs。 這必須安裝在已安裝jupyter
的環境中(您只需要一個),例如,如果這是base ,則
conda install -n base nb_conda_kernels
如果您使用的是 Jupyter 的系統級安裝(即不是由 Conda 安裝),那么您需要手動注冊您的 kernel。
conda activate r_venv_conda
Rscript -e 'IRkernel::installspec(name="ir33", displayname="R 3.3")'
您可以在其中為name
和displayname
設置自己的值。 有關詳細信息,請參閱 IRkernel 。
如果再次使用 Conda 安裝的 Jupyter,它只需要安裝在單個 env中。 這是在運行jupyter notebook
之前應該激活的環境。 一旦運行,然后你 select 你想使用 kernel。
我發現對我來說最好的方法是
conda create -n viper r python=3.8.8 #check your conda python version
conda activate viper
conda install -c r r-essentials
這將設置那些有用的 r 包,您可以稍后安裝更多。 並授予對 r-Kernel 的訪問權限。
我使用這些 bash 命令創建 R 環境並將其連接到 jupyter session:
# 1. pick a name for the conda environment
name='r_env'
# 2. create the environment
conda create -n $name r-essentials r-base
# 3. install `irkernel` and `devtools` for `IRkernel/repr`
conda install -c conda-forge r-devtools r-irkernel
# 4. setup `irkernel`
Rscript -e 'IRkernel::installspec(name="$name", displayname="$name")'
# 4. install and setup `IRkernel/repr` (for displaying help messages in jupyter)
Rscript -e 'devtools::install_github("IRkernel/repr")'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.