当前位置:网站首页>How to switch ipykernel to a different CONDA virtual environment in jupyterlab?
How to switch ipykernel to a different CONDA virtual environment in jupyterlab?
2022-06-30 02:42:00 【Ning Meng, Julie】
Content abstract : When learning virtual environment configuration , You may have seen it in JupyterLab ( or Jupyter Notebook) Different switching in kernel How to do it , It looks very cool . that , What's the use of this ? Can virtual environments be used more easily ? How can I achieve this function ? This article will give you a detailed introduction to .
The foundation of this article is to use conda Configure virtual environment , Welcome to my article : Use conda Configure the virtual environment and manage the installation package .
To learn the Anaconda After the virtual environment is configured , I see a running environment configuration file (environment.yml) A tutorial for , Just want to install the virtual environment . I thought I had mastered conda Various operations for configuring the virtual environment , I didn't expect to encounter new problems soon . stay “Hands-On Machine Learning with Scikit-Learn, Keras & TensorFLow, 2nd Edition, by Aurelien Geron(O’Reilly)” In the accompanying code of this book ( Code GitHub Address ), The author wrote a detailed installation guide , Starting steps and Use conda Configure the virtual environment and manage the installation package The method introduced in the article is no different , Also use conda env create hold .yml On the environment configuration installation , Create a “tf2” Virtual environment for . However , The next step is to tf2 Environment , Input :
(tf2)python3 -m ipykernel install --user --name=python3
It is said that... Can be set Jupyter Notebook Of kernel, open Jupyter Namely tf2 Environment configuration of . This makes me very confused , I was in a virtual environment before ( Like here “tf2”) Run in JupyterLab,import packages The version numbers of are all right . Why should I install again ipykernel Well ? and , I suddenly found that ,import packages The referenced library and its version number are still based on base Environmental Science , What's going on ?
stackoverflow This question and answer Answered my question . original , There are three ways to use different virtual environment configurations JupyterLab ( or Jupyter Notebook) . Let's combine tf2 Use this example to illustrate .
Method 1
Just as mentioned above 、 The way I used to . There are three steps :1. Enter the virtual environment 2. install jupyterlab 3. In this virtual environment , function jupyterlab.
install :
(base)conda activate tf2
(tf2)conda install jupyterlab
Use :
(tf2)jupyter lab
This method is simple and easy to operate , But there are two disadvantages :
1. Every time you have to enter the virtual environment first , Run the code again .
2. Every virtual environment should be installed jupyterlab ( or jupyter notebook), In fact, different virtual environments jupyter Between just running python Code kernel Different (kernel It can be understood as jupyter Used to run code in python Interpreter ), The editor and other frame parts are the same .
In this case , Can I use both base The framework of the environment (jupyterlab), The inside of kernel Just change it , This is the idea of method 2 .
Method 2
This method needs to be installed in the virtual environment first ipykernel And associated to Jupyter in , After the base Environment on Jupyter , You can also configure the virtual environment .
install :
(base)conda activate tf2
(tf2)conda install ipykernel
(tf2)ipython kernel install --user --name=tf2
Be careful :kernel The name does not have to be exactly the same as the virtual environment name , For example, here can also be :--name=tf2-kernel , But it is better to be similar or related to the virtual environment name , Otherwise, I may not remember which one kernel Which environment does it correspond to .
Use :
Can be in base Environment on JupyterLab , As shown below ,
(base)jupyter lab
By switching kernel Can be in JupyterLab Use in tf2 Environment configuration , Without having to enter tf2 Environment . The specific operation is : open JupyterLab , Click on kernel term , As shown in the figure below , You can see different... In the drop-down box kernels, Select the... Corresponding to the current environment kernel, Like here “tf2”, You can switch to tf2 Environmental Science .

This is actually quite convenient , stay base Open... In the environment JupyterLab You can switch between multiple environments kernel.
Can you further simplify kernel To configure ? As long as the virtual environment is installed ipykernel( You can install when you create a new virtual environment ), Automatic configuration kernel, There is no need to write in the virtual environment ipython kernel install --user --name=KernelName . This is the idea of method 3 .
Method 3
install :
stay base Environment , Input :
(base) conda install nb_conda_kernels
An automated tool is installed , Realization ipython kernel install The function of . in the future , In the new virtual environment, as long as ipykernel, Of the environment Jupyter kernel It will automatically create .
for example , Create a new one called “test” Virtual environment for , Install when creating a new environment ipykernel,
(base) conda create -n test ipykernel
And then you don't have to worry ,test Environmental kernel It will generate automatically , Isn't it easy? Ha ha .
Use :
Same as method 2 , stay base Environment on JupyterLab that will do , Will see kernel Option added “Python [conda env: test-kernel]”, Choose the kernel ,Jupyter Use the corresponding virtual environment configuration .
If you use JupyterLab, You will also see the newly generated differences on the initial page kernels, As shown in the figure below . If you want to create a new Jupyter Notebook, Click on a kernel, A blank of the virtual environment configuration can be generated .ipynb file .

After reading the introduction of the above three methods , You may already know the answer to question one . Why are you faking it? ipykernel? This is the difference between method 1 and method 2 . installed ipykernel, Can be in base Environment on JupyterLab Use this virtual environment configuration , It is not necessary to open the virtual environment every time JupyterLab. The effect of the two methods is the same .
To study the ipykernel after , There may be new questions : Why use method 1 only by default Python3 kernel, Nothing else kernel Optional , Will my environment configuration be base Environmental Science ?
Can't , I tested , Because the first method is to open in the virtual environment Jupyter, This environment configuration is used , You can use it. :
import package-name
print(package-name.__version__)
Check the installation package Version of , It is the virtual environment, not base Version of environment .
Back to question two ,import packages The version number of is still used base Environmental Science , What's going on ?
I analyze , The problem may be python3 -m ipykernel install --user --name=python3 Name in --name=python3. The installation guide mentions , In this way, we can put tf2 Of kernel Set to default kernel, No switching is required for each opening kernel. But let tf2 Of kernel and base Environmental kernel The names are the same , result kernel still base Environment configuration . stay Anaconda Prompt Input jupyter kernelspec list see Jupyter Of kernels, Only one... Is displayed kernel: python3. Use different names , such as python3 -m ipykernel install --user --name=tf2, And then there is tf2 Configuration environment kernel . Why? tf2 Of ipykernel use python3 name , Does not change the default Python3 kernel Environment configuration of , I haven't understood this problem yet , Welcome to communicate and give advice , thank you !
If this article is helpful to you , Please praise and support , thank you !
Pay attention to me Ning Meng Julie, learn from each other , Communicate more !
Reference article :
To configure kernel Three ways :
1.https://stackoverflow.com/questions/58068818/how-to-use-jupyter-notebooks-in-a-conda-environment/58068850
2.https://blog.csdn.net/u014264373/article/details/119390267
JupyterLab install ipykernel :
3.https://zhuanlan.zhihu.com/p/478628221
边栏推荐
- Recommendations for agileplm database parameter optimization
- 代码签名、驱动签名的常见问题解答
- What is certificate transparency CT? How to query CT logs certificate logs?
- 论文回顾:Playful Palette: An Interactive Parametric Color Mixer for Artists
- What are the three paradigms of database
- 可视化HTA窗体设计器-HtaMaker 界面介绍及使用方法,下载 | HTA VBS可视化脚本编写
- Template parameter package and function parameter package
- How to prevent phishing emails? S/mime mail certificate
- SSL证书七大常见错误及解决方法
- [论]【DSTG】Dynamic SpatiotemporalGraph Convolutional Neural Networks for Traffic Data Imputation
猜你喜欢

Precautions for purchasing wildcard SSL certificate
![[Postgres] Postgres database migration](/img/45/7074aa766640160a3b6f00b109cb2f.png)
[Postgres] Postgres database migration

FDA邮件安全解决方案

Intel-Hex , Motorola S-Record 格式详细解析
![[dry goods sharing] the latest WHQL logo certification application process](/img/c3/37277572c70b0af944e594f0965a6c.png)
[dry goods sharing] the latest WHQL logo certification application process

Merge sort

FDA ESG regulation: digital certificate must be used to ensure communication security

重看《Redis设计与实现》后记录几个要点

Recursion frog jumping steps problem

什么是证书透明度CT?如何查询CT logs证书日志?
随机推荐
隐藏在科技教育中的steam元素
HTA introductory basic tutorial | GUI interface of vbs script HTA concise tutorial, with complete course and interface beautification
VScode如何Debug(调试)进入标准库文件/第三方包源码
FDA ESG规定:必须使用数字证书保证通信安全
Azure 开发者新闻快讯丨开发者6月大事记一览
重看《Redis设计与实现》后记录几个要点
Enlightenment from the revocation of Russian digital certificate by mainstream CA: upgrade the SSL certificate of state secret algorithm to help China's network security to be autonomous and controlla
Redis+AOP怎么自定义注解实现限流
[论]【DSTG】Dynamic SpatiotemporalGraph Convolutional Neural Networks for Traffic Data Imputation
Differences among digicert, SECTIONO and globalsign code signing certificates
Quick sort
IBM websphere通道联通搭建和测试
Cmake tutorial series -05- options and variables
Seven common errors of SSL certificate and their solutions
What is a dangling pointer- What is a dangling pointer?
Entering Jiangsu writers and poets carmine Jasmine World Book Day
Jupyter notebook displays a collection of K-line graphs
AutoJS代碼能加密嗎?YES,AutoJS加密技巧展示
Unity TimeLine 数据绑定
Steam elements hidden in science and Technology Education