Python 如何将 conda 环境添加到 jupyter 实验室
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53004311/
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
How to add conda environment to jupyter lab
提问by Statistic Dean
I'm using Jupyter Lab and I'm having trouble to add conda
environment. The idea is to launch Jupyter Lab from my base environment, and then to be able to choose my other conda envs as kernels.
我正在使用 Jupyter Lab,但在添加conda
环境时遇到问题。这个想法是从我的基本环境启动 Jupyter Lab,然后能够选择我的其他 conda 环境作为内核。
I installed the package nb_conda_kernels
which is supposed to do just that, but it's not working as I want. Indeed, let's assume I create a new Conda Environment, then I launch jupyter lab from base, I can't see the new environment as an available kernel.
我安装了nb_conda_kernels
应该这样做的软件包,但它没有按我想要的方式工作。事实上,让我们假设我创建了一个新的 Conda 环境,然后我从基础启动 jupyter 实验室,我无法将新环境视为可用内核。
I have found a "fix", which works everytime but is not convenient at all. If I install Jupyter Notebook in my new environment, then launch a jupyter notebook from this new environment, close it, go back to base environment, and then launch Jupyter Lab from base environment, my new environment is available as a kernel in Jupyter Lab.
我找到了一个“修复”,它每次都有效,但根本不方便。如果我在我的新环境中安装 Jupyter Notebook,然后从这个新环境启动一个 jupyter notebook,关闭它,回到基础环境,然后从基础环境启动 Jupyter Lab,我的新环境可以作为 Jupyter Lab 的内核使用。
If you know how to make it work without this "fix", I would be very grateful.
如果您知道如何在没有此“修复”的情况下使其工作,我将不胜感激。
回答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 Statistic Dean
A solution using nb_conda_kernels
. First, install it in your base environment :
使用nb_conda_kernels
. 首先,将其安装在您的基本环境中:
(base)$ conda install -c conda-forge nb_conda_kernels
Then in order to get a kernel for the conda_env cenv
:
然后为了获得 conda_env 的内核cenv
:
$ conda activate cenv
(cenv)$ conda install ipykernel
(cenv)$ conda deactivate
You will get a new kernel named Python [conda env:cenv]
in your next run of jupyter lab
/ jupyter notebook
您将Python [conda env:cenv]
在下一次运行jupyter lab
/ 时获得一个新内核jupyter notebook
Note :
If you have installed nb_conda_kernels
, and want to create a new conda environment and have it accessible right away then
注意:如果您已经安装nb_conda_kernels
,并且想要创建一个新的 conda 环境并立即访问它,那么
conda create -n new_env_name ipykernel
will do the job.
会做的工作。