Import torch or other packages, but Jupyter notebook does not display the reason to solve | Solve the problem of Jupyter Notebook: no module named but actually pip install

Solve the problem of Jupyter Notebook: no module named... but actually pip install

When some packages (such as) are installed in the kernel of jupyter notebook import torchbut jupyter notebook shows that there is no such package, sort out and solve the relevant reasons.

Picture 1

  1. Base environment problem
    When we install Anaconda and want to use jupyter notenook, at this time, typing jupyter notenook in the terminal (base environment) will jump to the jupyter notenook web page for us to use.
    insert image description here
    At this time, the existing kernel will be displayed in the upper right corner of the web page. When the virtual environment created under Anaconda is not imported into the jupyter kernel, the base environment kernel will be displayed as ipykernel. At this time, there is only this one, and you can open it at will. See the kernel in kernel in the jupyter file .

insert image description here
insert image description here
At this time, import torch will display the problem in Figure 1. We only need to install pytorch in the base environment.

pip install torch==1.8.1+cu102 torchvision==0.9.1+cu102 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html

Or go to pytorch official website to download https://pytorch.org/get-started/previous-versions/

After downloading the corresponding package, you can use it directly.

  1. Virtual environment issues - important! ! !
    ——— Add a virtual environment as the Jupyter notebook kernel , and you must enter and add it in this virtual environment .

Generally, we will create a virtual environment directly in Anaconda. At this time, it is very convenient to use jupyter to directly import the virtual environment as the kernel. You can import the virtual environment into jupyter through the following command

① First check how many kernels there are under jupyter:
at the same time, first make sure that there is ipykernel ipythona library in the environment, if not, install it

# 安装ipykernel ipython
pip install ipykernel ipython

# 查看jupyter下面有多少个kernels
jupyter kernelspec list

② Add a virtual environment as the kernel in Jupyter notebook:

ipython kernel install --user --name pytorch1.6(虚拟环境名字)

Generally, at this time, check how many kernels there are under jupyter: it will show that the relevant virtual environment has been added to the kernel,
insert image description hereand at the same time, we will also show that the corresponding kernel can be used on the jupyter web page.

But at this time, if the problem of ModuleNotFoundError: No module named 'torch'no , it may be because the operation of adding the virtual environment as the kernel in the above Jupyter notebook is performed in the base environment. Please remember to activate and enter the one you want to add first. Virtual environment, import the environment into the jupyter kernel in the virtual environment!

conda activate pytorch1.6(虚拟环境名字)

# 在Jupyter notebook中添加虚拟环境作为内核:
ipython kernel install --user --name pytorch1.6(虚拟环境名字)

At this time, restarting jupyter basically solves the problem.
③Delete the specified kernel command:

jupyter kernelspec remove pytorch1.6(虚拟环境名字)

Guess you like

Origin blog.csdn.net/m0_56075892/article/details/130005168
Recommended