'Can't import torch in jupyter notebook
System: macOS 10.13.6 Python: 3.7 Anaconda3
I have trouble when import torch in jupyter notebook.
ModuleNotFoundError: No module named 'torch'
Here is how I install pytorch:
conda install pytorch torchvision -c pytorch
I've checked PyTorch is installed in my anaconda environment:
When I command python3 in my terminal and import torch, it works. But not work in jupyter notebook
I've tried:
conda update conda
conda install mkl=2018
But still the same error.
Solution 1:[1]
You have to install jupyter in addition to pytorch inside your activated conda env. Here is installation steps:
1. Create conda env
for example: pytorch_p37 with python 3.7:
user@pc:~$ conda create -n pytorch_p37 python=3.7
2. Activate it
user@pc:~$ conda activate pytorch_p37
Or with (for older conda versions):
user@pc:~$ source activate pytorch_p37
Now you should see (pytorch_p37) before your shell prompt:
(pytorch_p37) user@pc:~$
3. Go to PyTorch website and choose appropriate installation command via conda. Run it in your shell, for example
(pytorch_p37) user@pc:~$ conda install pytorch torchvision -c pytorch
4. Install jupyter inside your activated env as well
(pytorch_p37) user@pc:~$ conda install jupyter
5. Verify the installation
(pytorch_p37) user@pc:~$ conda list
# packages in environment at /home/user/anaconda3/envs/pytorch_p37:
#
# Name
...
jupyter 1.0.0
jupyter_client 5.3.1
jupyter_console 6.0.0
jupyter_core 4.5.0
...
python 3.7.4
pytorch 1.2.0
...
6. Run jupyter
(pytorch_p37) user@pc:~$ jupyter notebook
Solution 2:[2]
I had the same problem where the jupyter lab couldn't import torch after successful installation in my virtual environment. But when I checked the list of installed packages using conda list, PyTorch was present but not jupyterlab. After installing jupyterlab, it is working fine!.
Solution 3:[3]
I experienced the same bug. I tried the following line on anaconda command prompt.
(pytorch)C:\Users\user> conda install jupyter
Then, I used the command C:\Users\user> jupyter notebook which launched the Jupyter notebook, and the import torch keyword started to work.
Solution 4:[4]
Just fixed the problem. My environment is that windows10, conda 4.8.3, and python 3.8.3. Here are what I do,
- Enter anaconda prompt shell and activate a environment of anoconda. My environment is pytorch.
activate pytorch
- Create a kernel for Jupyter notebook in anoconda prompt shell for linking the environment of anoconda to a Jupyter notebook's kernel.
python -m ipykernel install --user --name pytorch --display-name "pytorch"
The first pytorch is the name of environment of anoconda, the second is the name of kernel of Jupyter notebook. Make sure ipykernel installed.
3.Run Jupyter notebook, select the kernel you just created in step 2, then import the module of torch to see the result.
Solution 5:[5]
I have just resolved this problem by runing conda install pytorch torchvision -c pytorch on Anaconda Powershell Prompt.
Actual Result
(base) PS C:\Users\TayyabChaudhary> conda install pytorch torchvision -c pytorch
Collecting package metadata (current_repodata.json): done
Solving environment: done
## Package Plan ##
environment location: C:\Users\TayyabChaudhary\anaconda3
added / updated specs:
- pytorch
- torchvision
The following packages will be downloaded:
package | build
---------------------------|-----------------
ca-certificates-2021.9.30 | haa95532_1 116 KB
certifi-2021.10.8 | py38haa95532_0 152 KB
conda-4.10.3 | py38haa95532_0 2.9 MB
cudatoolkit-11.3.1 | h59b6b97_2 545.3 MB
libuv-1.40.0 | he774522_0 255 KB
openssl-1.1.1l | h2bbff1b_0 4.8 MB
pytorch-1.10.0 |py3.8_cuda11.3_cudnn8_0 1.44 GB pytorch
pytorch-mutex-1.0 | cuda 3 KB pytorch
torchvision-0.11.1 | py38_cu113 8.8 MB pytorch
------------------------------------------------------------
Total: 1.99 GB
The following NEW packages will be INSTALLED:
cudatoolkit pkgs/main/win-64::cudatoolkit-11.3.1-h59b6b97_2
libuv pkgs/main/win-64::libuv-1.40.0-he774522_0
pytorch pytorch/win-64::pytorch-1.10.0-py3.8_cuda11.3_cudnn8_0
pytorch-mutex pytorch/noarch::pytorch-mutex-1.0-cuda
torchvision pytorch/win-64::torchvision-0.11.1-py38_cu113
The following packages will be UPDATED:
ca-certificates anaconda::ca-certificates-2020.10.14-0 --> pkgs/main::ca-certificates-2021.9.30-haa95532_1
certifi anaconda::certifi-2020.6.20-py38_0 --> pkgs/main::certifi-2021.10.8-py38haa95532_0
conda 4.10.1-py38haa95532_1 --> 4.10.3-py38haa95532_0
openssl 1.1.1k-h2bbff1b_0 --> 1.1.1l-h2bbff1b_0
Proceed ([y]/n)? y
Downloading and Extracting Packages
libuv-1.40.0 | 255 KB | ############################################################################ | 100%
torchvision-0.11.1 | 8.8 MB | ############################################################################ | 100%
certifi-2021.10.8 | 152 KB | ############################################################################ | 100%
cudatoolkit-11.3.1 | 545.3 MB | ############################################################################ | 100%
openssl-1.1.1l | 4.8 MB | ############################################################################ | 100%
pytorch-1.10.0 | 1.44 GB | ############################################################################ | 100%
conda-4.10.3 | 2.9 MB | ############################################################################ | 100%
ca-certificates-2021 | 116 KB | ############################################################################ | 100%
pytorch-mutex-1.0 | 3 KB | ############################################################################ | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
Solution 6:[6]
enter image description herePersonllay, I have tried different solutions and it solves my problem.
First, enter anaconda prompt and use the command conda install nb_conda.
Second, enter the env of pytorch and use conda install ipykernel.
After this, we can find in jupyter notebook, we have more language to use. Choose the language Python [conda env:conda-pytorch], then we can run code using pytorch successfully.
Hope it can help you!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Ankita Jaiswal |
| Solution 3 | Debjit Bhowmick |
| Solution 4 | Nio Zhu |
| Solution 5 | Tayyab Chaudhary |
| Solution 6 |

