The most elegant and concise VsCode method to configure Jupyter multi-version Python kernel (not involving Anaconda)

Continuing from the previous note, when we have installed multiple versions of Python interpreters, we want to use Jupyter in VsCode to run codes written in different versions of Python.

You can do it like this:

1.VsCode installs Jupyter extension plug-in

Click the extension plug-in button on the left and enter [Jupyter] in the search box. By default, the Jupyter plug-in released by Microsoft will be displayed first. Click [Install]

2.Python installs ipykernel kernel

Taking the Python3.11 version as an example, [win+r] enter [cmd] to open the command prompt window, and enter the following command to install ipykernel:

pip3.11 install ipykernel

 3. Return to VsCode, create the ipynb file, and select the Python interpreter in the upper right corner

Go back to VsCode and create a new file ending with [.ipynb]. This file format is the storage format of the python code written in jupyter notebook. Click [Select Kernel] in the upper right corner (since I have already selected my python3.11 explanation here) The tool displays the Python version number. If no selection has been made, "Select Kernel" appears by default).

 Then pay attention! ! ! Then pay attention! ! ! Then pay attention! ! ! Here you must select the python version of ipykernel that you just installed using the pip command! Here you must select the python version of ipykernel that you just installed using the pip command! Here you must select the python version of ipykernel that you just installed using the pip command!

 4. Run the code

Write Python code. Here are two simple for loops. Click Run All above and you can see that the codes are all running normally! In this way, we have configured a Jupyter environment that can run the Python3.11 kernel in VsCode.

5. ipykernel is not installed and error is reported when running

If we had just chosen a Python version that did not install ipykernel using the pip command when selecting the Python interpreter version, then we would get an error when we run the Python code, as follows:

Here I chose the Python3.10 version interpreter that has not installed ipykernel as the kernel. You can see that the code cannot run normally.

 The solution is very simple, [win+r] enter [cmd] to open the command prompt, enter the following command, press Enter, and install ipykernel for Python 3.10.

pip3.10 install ipykernel

After the installation is complete, go back to VsCode and pay attention to the upper right corner. The kernel I selected is still Python3.10, but when I run the code, everything can run normally!

 Written at the end: The configuration method of the virtual environment is also the same. If Python is installed with ipykernel in your computer before creating the virtual environment, then after the virtual environment is created, the virtual Python environment will also have ipykernel. At this time, pay attention to using the virtual environment. environment name or path.

Originally I used Anaconda for development, but later I felt that Anaconda was not very convenient, because Anaconda could not download some specific newer versions of third-party libraries, and many colleagues used VsCode to write code after work, so I installed VsCode. Multiple versions of the Python interpreter are also installed. The online tutorials on how to use VsCode to configure the Jupyter kernel basically do not teach you how to deal with multiple versions of Python. If you query Jupyter to configure multiple versions of the Python kernel, the results are basically Anaconda, so you have no choice but to do it yourself. I checked the tutorials and explored on my own, and this article is today’s result.

Supongo que te gusta

Origin blog.csdn.net/qq_29108959/article/details/131717713
Recomendado
Clasificación