This article is quite wordy, I like to describe the “past and future” of a problem clearly (readers have patience can be as a story/funny. JPG), to see the solution directly click the directory jump can be. To recap the point of this article: why can JupyterNotebook import Torch but Not JupyterLab in a Pytorch installed virtual environment? JupyterLab is a new generation of JupyterNotebook. How should I configure it to use the Torch framework?

In a previous article I wrote a detailed tutorial on how to install Anaconda and how to configure the virtual environment you need. Anaconda offers great convenience to machine learners, at least without the semblance of metaphysics when it comes to managing frameworks and packages. The Anaconda tutorial is here to stay: This is probably the most thoughtful Anaconda tutorial ever written. Hopefully, you’ll get a lot out of it.

With the help of Anaconda, I wrote a lot of neural network models for deep learning, and also started to do some data mining, integrated learning and YoloV5 target detection. For larger projects like object detection and deep learning, I choose to use Pycharm for programming, but when it comes to block data analysis, I go back to Jupyter, which helped me learn Python syntax.

I activate Pytorch_envs from CMD (this is the virtual environment where I installed PyTorch) and then type Jupyter Lab to open it.

Then I click on the Python 3 icon above and go straight to brute import!Like the picture above, I’m surprised, I’m surprised, I’m flustered! I have no problem using Pycharm Spyder JupyterNotebook in my own virtual environment, why JupyterLab is wrong? I looked up a lot of information, some friends said, “this JupyterNotebook and JupyterLab is a family, there is no difference!” “This Jupyter is only able to adjust the base environment! To use a torch, you have to install a torch at the base!” . First, let’s prove the first statement wrong: Same environment, same path, same way to start JupyterNotebook. Let’s run the program that just failed import Torch. My torch is only installed in Pytorch_envs. The base environment remains unchanged


Let’s formally explain how to use torch in JupyterLab

Import the Pytorch environment with the virtual environment and Pytorch installed!

Activate < virtual environment name >#Install IPython kernelfor Jupyter
conda install ipykernel -y
#Import the PyTorch virtual environment into the IPykernelPython -m ipykernel install --user --name < virtual environment name > --display-name "python PyTorch"Copy the code

After successfully executing the command, let’s start JupyterLab again to see the effect.In the figure above, there are two extra terms after Python 3, i.ePython -m ipykernel install --user --name < virtual environment name > --display-name "python PyTorch"The name in the last double quotation mark in this command! Click into edit mode to see if we can finally achieve the desired effect.Perfect closure ⭐

The solution to this problem, as I understand it, is simple:

Integrate the virtual environment with Pytorch into the Jupyter kernel! Done!

Reference: Environmental preparation of PyTorch in JupyterLab