简体   繁体   English

如何在 Jupyter Notebook 上为新笔记本切换环境?

[英]How to switch environment on Jupyter notebook for new notebook?

I have an instance with various environment and some notebooks are compatible with different environment for eg notebook1 is for MXNet and notebook2 is for Tensorflow.我有一个具有各种环境的实例,一些笔记本与不同的环境兼容,例如 notebook1 用于 MXNet,notebook2 用于 Tensorflow。

How do I jump to new environment when I move from notebook1 to notebook2?当我从 notebook1 移动到 notebook2 时,如何跳转到新环境? I tried doing that but this doesn't quite work with Jupyter notebook?我试过这样做,但这对 Jupyter 笔记本不太适用? Any suggestion?有什么建议吗?

So I need to do it from conda environment but looks like jupyter notebook UI doesn't respect (calls right activation function) to set the path.所以我需要在 conda 环境中进行,但看起来 jupyter notebook UI 不尊重(调用正确的激活函数)来设置路径。

You could use the nb_conda_kernels package, which provides a separate jupyter kernel for each conda environment, along with the appropriate code to handle their setup.您可以使用nb_conda_kernels包,它为每个 conda 环境提供单独的 jupyter 内核,以及处理它们的设置的适当代码。 This makes switching conda environment as simple as switching jupyter kernel (eg from the kernel menu), which I find very convenient.这使得切换 conda 环境就像切换 jupyter 内核(例如从内核菜单)一样简单,我觉得这非常方便。 You can get it from the conda-forge channel, using您可以从 conda-forge 频道获取它,使用

conda install -c conda-forge nb_conda_kernels

When you start a jupyter notebook within an environment, it will only have access to the modules installed in that particular enviroment.当您在环境中启动 jupyter notebook 时,它只能访问安装在该特定环境中的模块。 If you need two specific environments for two different notebooks, you will need to start a jupyter notebook within the two environments separately.如果两个不同的 notebook 需要两个特定的环境,则需要在两个环境中分别启动一个 jupyter notebook。

If you are using anaconda, you would do the following in a terminal:如果您使用 anaconda,您将在终端中执行以下操作:

source activate MXNET
jupyter notebook

Then in a second terminal:然后在第二个终端:

 source activate Tensorflow
 jupyter notebook

除了这个包,我认为还需要使用以下配置更改 ~/.jupyter/jupyter_notebook_config.py 文件

c.NotebookApp.kernel_spec_manager_class = 'environment_kernels.EnvironmentKernelSpecManager'

I don't really know the definition and relationship between kernel and conda env , but I beleive we can set a kernel for each env .我真的不知道kernelconda env之间的定义和关系,但我相信我们可以为每个env设置一个内核。

I got a problem similar to you: I clean my data in data_cleaning.ipynb under env_without_lgb .我遇到了与您类似的问题:我在data_cleaning.ipynbenv_without_lgb数据。 However, I have lightgbm installed under env_lightgbm .但是,我在lightgbm下安装了env_lightgbm So, I want to switch kernel/env from env_without_lgb to env_lightgbm , which we can use Kernel -> Change Kernel :所以,我想将kernel/envenv_without_lgbenv_lightgbm ,我们可以使用Kernel -> Change Kernel

在此处输入图片说明

If there is not a kernel you want, create a kernel under the right env according to this guide .如果没有您想要的内核,请根据本指南在正确的 env 下创建一个内核。 For me, I only have a Pure Python 3 kernel and R kernel at first and then I create a kernel under env_lightgbm .对我来说,我一开始只有一个Pure Python 3 kernelR kernel ,然后我在env_lightgbm下创建了一个内核。 At last, we can switch kernel when we editing our code.最后,我们可以在编辑代码时切换内核。

However, I still can not figure out the relationship between kernel and env.但是,我仍然无法弄清楚内核和环境之间的关系。

Installing the nb_conda_kernels package as suggested by user @jcb91 worked for me.按照用户@jcb91 的建议安装nb_conda_kernels包对我有用。 I did have to install it both in my root (base) environment as well as the virtual environment with which I wanted to use it.我确实必须在我的根(基本)环境以及我想使用它的虚拟环境中安装它。 To do this, run the following in the Anaconda prompt (in your base environment):为此,请在 Anaconda 提示符下(在您的基本环境中)运行以下命令:

conda install nb_conda_kernels

Then, activate your virtual environment (in the code below named 'myenv') and install the nb_conda_kernels package there as well.然后,激活您的虚拟环境(在下面名为“myenv”的代码中)并在那里安装nb_conda_kernels包。 You can do this by running in the Anaconda prompt:您可以通过在 Anaconda 提示符下运行来执行此操作:

conda activate myenv
conda install nb_conda_kernels

You now should be able to switch to your different environment using:您现在应该能够使用以下方法切换到不同的环境:

Kernel -> Change Kernel

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

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