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.
You have to install jupyter
in addition to pytorch
inside your activated conda env. Here is installation steps:
for example: pytorch_p37
with python 3.7
:
user@pc:~$ conda create -n pytorch_p37 python=3.7
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:~$
conda
. Run it in your shell, for example(pytorch_p37) user@pc:~$ conda install pytorch torchvision -c pytorch
jupyter
inside your activated env as well (pytorch_p37) user@pc:~$ conda install jupyter
(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
...
jupyter
(pytorch_p37) user@pc:~$ jupyter notebook
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!.
I experienced the same bug. I tried the following line on anaconda command prompt.
(pytorch)C:\\Users\\user\u0026gt; conda install jupyter
Then, I used the command C:\\Users\\user\u0026gt; jupyter notebook
which launched the Jupyter notebook, and the import torch keyword started to work.
Just fixed the problem. My environment is that windows10, conda 4.8.3, and python 3.8.3. Here are what I do,
activate pytorch
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.
I have just resolved this problem by runing conda install pytorch torchvision -c pytorch
on Anaconda Powershell Prompt .
(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
enter image description here Personllay, 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!
I understand creating a virtual environment is an ideal approach but by installing a jupyter notebook extension in VSCode helped me solve this issue. I did not have to create a virtual environment (This depends on the user if they have to create one or not)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.