当前位置:网站首页>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
边栏推荐
- [dry goods sharing] the latest WHQL logo certification application process
- CMake教程系列-01-最小配置示例
- PEM_ read_ bio_ Privatekey() returns null only in ECB mode - PEM_ read_ bio_ PrivateKey() returns NULL in ECB mode only
- 代码签名、驱动签名的常见问题解答
- 银行的理财产品一般期限是多久?
- JS advanced -es6 syntax
- 最小栈详解
- Raki's notes on reading paper: named entity recognition as dependency parsing
- Série de tutoriels cmake - 02 - génération de binaires à l'aide du Code cmake
- Raii memory management
猜你喜欢

Precautions for purchasing wildcard SSL certificate

打造創客教育中精湛技藝

五个最便宜的通配符SSL证书品牌

Pytoch learning (II)

A quick look at the statistical data of 23 major cyber crimes from 2021 to 2022

Merge sort

CMake教程系列-02-使用cmake代碼生成二進制

FDA邮件安全解决方案

Call collections Sort() method, compare two person objects (by age ratio first, and by name ratio for the same age), and pass lambda expression as a parameter.

JMeter obtains cookies across thread groups or JMeter thread groups share cookies
随机推荐
Unity3d ugui force refresh of layout components
LeetCode 3. 无重复字符的最长子串
选购通配符SSL证书注意事项
Cmake tutorial series -05- options and variables
Créer des compétences exquises dans l'éducation des créateurs
CMake教程系列-01-最小配置示例
Two methods of SSL certificate format conversion
FAQs for code signature and driver signature
Xunwei NXP itop-imx6 development platform
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
Pytoch learning (II)
SQL injection -day17
Time complexity analysis
JMeter obtains cookies across thread groups or JMeter thread groups share cookies
What is a dangling pointer- What is a dangling pointer?
Recursion frog jumping steps problem
Wechat applet page Jump and parameter transfer
Global and Chinese markets for light cargo conveyors 2022-2028: Research Report on technology, participants, trends, market size and share
Digicert、Sectigo、Globalsign代码签名证书的区别
论文回顾:Playful Palette: An Interactive Parametric Color Mixer for Artists