Pytorch: ModuleNotFoundError: No module named 'torch'

Created on 24 Jan 2018  ·  57Comments  ·  Source: pytorch/pytorch

  • OS: macOS High Sierra version 10.13.2
  • PyTorch version:
  • How you installed PyTorch (conda, pip, source): pip3
  • Python version: Python 3.6.0 :: Anaconda 4.3.0 (x86_64)
  • CUDA/cuDNN version: No GPU

I successfully installed torch and torchvision using pip3 but

when type "import torch" in jupyter notebook i get the following

ModuleNotFoundError Traceback (most recent call last)
in ()
----> 1 import torch

ModuleNotFoundError: No module named 'torch'

import sys
print(sys.executable)
/Users/user/anaconda3/bin/python

image

Most helpful comment

@vishwakftw for PackageNotFoundError I tried this:
$conda update conda
$conda install mkl=2018
which I found here
and it works now
Thanks vishwakftw

All 57 comments

Since you have conda, could you try installing using conda?

@vishwakftw conda install pytorch torchvision -c pytorch
and got this
PackageNotFoundError: Package not found: '' Dependencies missing in current osx-64 channels:

  • pytorch -> mkl >=2018
  • torchvision -> pytorch >=0.3 -> mkl >=2018

@vishwakftw for PackageNotFoundError I tried this:
$conda update conda
$conda install mkl=2018
which I found here
and it works now
Thanks vishwakftw

I've the same error"ModuleNotFoundError: No module named 'torch'" when debugging my code.
How to handle it?
Thanks

@Jerrybonheur
If you have conda, try installing pytorch using conda (on mac for example):

$conda install pytorch torchvision -c pytorch

If this still dose not work, then try:

$conda update conda
$conda install mkl=2018

I have tried the three steps given by @xiaonengmiao, still "ModuleNotFoundError: No module named 'pytorch'"

Any ideas?

Faced similar issue in Windows 10 with Anaconda installed. Installed pytorch package using conda install command which I found here: http://pytorch.org/

The usual import from command-line worked for my environment(_torch_):

(torch) C:\Users\torch>python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>>  

But, when I tried to import this package from Jupyter notebook, I got following error message:

ModuleNotFoundError: No module named 'torch'

Then, I tried installing Jupyter notebook application from Anaconda navigator for my environment(_torch_).
Restarted my Jupyter notebook and ran import torch and this time it worked.

@nicospbr If it's still not resolved, run conda list and check whether jupyter is installed or not. If not, run conda install jupyter . Now, open jupyter notebook and run import torch .

pip3 install http://download.pytorch.org/whl/cpu/torch-0.4.0-cp36-cp36m-win_amd64.whl
do this for windows 10 its work fine @xiaonengmiao

For me installing these steps made it work:

1- installing -> conda install nb_conda
2 - installing -> conda update conda
3- installing conda install mkl=2018

This is what I found :

If you have more than one environment in Anaconda, jupyter, pytorch and torchvision need to be installed in the same environment. Otherwise, jupyter won't find the other 2 from where it is launched. Unless you know how to 'make jupyter aware' of the path to the environment where the other 2 reside, the fix is to have all 3 in the same environment.

@atan4583
Yes, oddly enough.. I had the following situation:

from a new console

  • create conda environment 'conda create -n test'
  • installed torch
  • run 'import torch' works

from a jupyter console

  • switch to the same environment 'source activate test'
  • conda list -> torch is already there (and just tested)
  • run 'import torch' in the jupyter console window fails

the Jupyter package must be installed in the base environment and not in this new environment. From inside a Jupyter console, if you do switch to the same environment 'source activate test', that does not update the Jupyter's PYTHONPATH at all.

True, but I tried to import in the same jupyter-lab console where I switched to the other environment. That is why it is odd.

mm.. It seems that I hit this issue here: https://github.com/jupyterlab/jupyterlab/issues/4672

import torch it is working well with jupyter notebook but it fails with jupyter-lab

@jleni , I'm also facing the same issues. import torch works fine with jupyter-notebook but not with jupyter-lab if I'm connecting to a remote server. But if I'm connecting to my local machine, then both work fine.

I have removed all the packages related to torch, then reinstalled. It worked for me since the conda mixed up the versions of the torch packages.

List torch related packages:
conda list | grep torch
Remove and reinstall the packages:
conda uninstall torchvision-cpu pytorch-cpu
conda install pytorch-cpu torchvision-cpu -c pytorch

The command to import pytorch is:

import torch
not
import pytorch

      The command to import pytorch is:

import torch
not
import pytorch

i am facing the first question. and any help will be thinks.

I installed follow link: https://pytorch.org/. It works for me

pip3 install http://download.pytorch.org/whl/cpu/torch-0.4.0-cp36-cp36m-win_amd64.whl
do this for windows 10 its work fine @xiaonengmiao

it will not work..it successfully installed torch but while running command import torch it showing module not found error

Faced similar issue in Windows 10 with Anaconda installed. Installed pytorch package using conda install command which I found here: http://pytorch.org/

The usual import from command-line worked for my environment(_torch_):

(torch) C:\Users\torch>python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>>  

But, when I tried to import this package from Jupyter notebook, I got following error message:

ModuleNotFoundError: No module named 'torch'

Then, I tried installing Jupyter notebook application from Anaconda navigator for my environment(_torch_).
Restarted my Jupyter notebook and ran import torch and this time it worked.

Hello, I can import torch both in jupyter and console. But when i try running a python file, it failed. Do you have some suggestions? Thanks
image

pip3 install http://download.pytorch.org/whl/cpu/torch-0.4.0-cp36-cp36m-win_amd64.whl
do this for windows 10 its work fine @xiaonengmiao

it will not work..it successfully installed torch but while running command import torch it showing module not found error

Have you figured out this issue? I guess i met the same problem as yours

same problem here. I installed pytorch but when i try to run it on any ide or text editor i get the "no module named torch". However, it does work in jupyter notebook and ipython (from cmd). Any possible solution?

I'm having the same issue.

It's slightly different in my case, since I was installing pytorch with anaconda without internet connection. Here's my script:

CONDA_PACKS="/path/to/conda_packages"
conda install --offline --verbose ${CONDA_PACKS}/ninja-1.8.2-h1ad3211_1001.tar.bz2
conda install --offline --verbose ${CONDA_PACKS}/resampy-0.2.1-py_1.tar.bz2
conda install --offline --verbose ${CONDA_PACKS}/pytorch-1.0.1-py3.7_cuda90_cudnn7_1.tar.bz2

Any ideas?

same problem here. I installed pytorch but when i try to run it on any ide or text editor i get the "no module named torch". However, it does work in jupyter notebook and ipython (from cmd). Any possible solution?

You need to configure the environment path for the anaconda python, then I think you can run in IDE. But I haven't found how to fun in command prompt yet.

Turns out that was version mismatch of installed plugins.

I used "conda install" command provided on the plugin page to check the exact version strings of the dependencies and then downloaded them one by one.

pip3 install http://download.pytorch.org/whl/cpu/torch-0.4.0-cp36-cp36m-win_amd64.whl
do this for windows 10 its work fine @xiaonengmiao

it will not work..it successfully installed torch but while running command import torch it showing module not found error

If you want to install pytorch inside a conda environment, please use pip instead of pip3.

I had the mkl-2019.4 and it still didn't work. I have installed the mkl 2018 and it is now fine.
conda install mkl=2018

Go to this page, choose your preferences, and it will give you the individualized, correct command to install on your particular system: https://pytorch.org/get-started/locally/

However, I ran into a very similar issue. I ended up fixing it by moving my project to a new Pycharm project (starting fresh), and even though I have Anaconda installed, the following commands worked for Pytorch and installed everything quickly and beautifully. My program now works:

pip3 install https://download.pytorch.org/whl/cpu/torch-1.1.0-cp37-cp37m-win_amd64.whl
pip3 install https://download.pytorch.org/whl/cpu/torchvision-0.3.0-cp37-cp37m-win_amd64.whl

I wish I had the resource I found these at. I was interrupted by a family matter. Luckily though, it worked for my project.

We can install torch version using anaconda GUI or conda terminal in desired environment but while running program, we need select that environment. It will work fine. if you are running program through terminal, please activate environment where you have installed torch version. Command for activating a environment: $conda activate my_env then run your program.

@jithinjk Can you please talk more about how you "installing Jupyter notebook application from Anaconda navigator for my environment(torch)"? Thank you.

I've tried above methods, but they do not work :(

Faced similar issue in Windows 10 with Anaconda installed. Installed pytorch package using conda install command which I found here: http://pytorch.org/
The usual import from command-line worked for my environment(_torch_):

(torch) C:\Users\torch>python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>>  

But, when I tried to import this package from Jupyter notebook, I got following error message:
ModuleNotFoundError: No module named 'torch'
Then, I tried installing Jupyter notebook application from Anaconda navigator for my environment(_torch_).
Restarted my Jupyter notebook and ran import torch and this time it worked.

Hello, I can import torch both in jupyter and console. But when i try running a python file, it failed. Do you have some suggestions? Thanks
image

Did you restart the Jupyter notebook?

@jithinjk Can you please talk more about how you "installing Jupyter notebook application from Anaconda navigator for my environment(torch)"? Thank you.

I've tried above methods, but they do not work :(

In the environment, which you created, install Jupyter Notebook application. You can do this through Anaconda Navigator if you have that installed. Please refer attached screenshot.

jup

Hi everybody!
what is my fault in this picture?
I wrote a python code, when I wanted to compile it I faced with problem.
i dont know what should i do.
please help me.
thanks.
image

Follow the instruction on the website, I don't need to install with the cuda tool, but need to install with tag torchvision-cpu or cudatoolkit=$, works for me.
https://pytorch.org/get-started/locally/

Hi all,
installed pytorch with conda :

(base) (3.8.0/envs/my_virtual_env-3.8.0) marco@pc:~/facenet_pytorch/examples$ conda install  
pytorch torchvision cpuonly -c pytorch
Collecting package metadata (current_repodata.json): done
Solving environment: done

# All requested packages already installed.

I updated conda:

(base) (3.8.0/envs/my_virtual_env-3.8.0) marco@pc:~/facenet_pytorch/examples$ conda update 
conda
Collecting package metadata (current_repodata.json): done
Solving environment: done

# All requested packages already installed.

Installed mkl=2019 :

(base) (3.8.0/envs/my_virtual_env-3.8.0) marco@pc:~/facenet_pytorch/examples$ conda install  
mkl=2019
Collecting package metadata (current_repodata.json): done
Solving environment: done

# All requested packages already installed.

(base) (3.8.0/envs/my_virtual_env-3.8.0) marco@pc:~/facenet_pytorch/examples$ conda list | grep 
torch
cpuonly                   1.0                           0    pytorch
facenet-pytorch           0.1.0                    pypi_0    pypi
pytorch                   1.3.0               py3.7_cpu_0  [cpuonly]  pytorch
torchfile                 0.1.0                    pypi_0    pypi
torchvision               0.4.1                  py37_cpu  [cpuonly]  pytorch

But it still says "no module torch" :

(base) (3.8.0/envs/my_virtual_env-3.8.0) marco@pc:~/facenet_pytorch/examples$ python3
Python 3.8.0 (default, Oct 30 2019, 16:20:23) 
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'torch'
>>>

Ubuntu 18.04.02 Server Edition

Any ideas to solve the problem?
Looking forward to your kind help.
Marco

Hi all,
installed pytorch with conda :

(base) (3.8.0/envs/my_virtual_env-3.8.0) marco@pc:~/facenet_pytorch/examples$ conda install  
pytorch torchvision cpuonly -c pytorch
Collecting package metadata (current_repodata.json): done
Solving environment: done

# All requested packages already installed.

I updated conda:

(base) (3.8.0/envs/my_virtual_env-3.8.0) marco@pc:~/facenet_pytorch/examples$ conda update 
conda
Collecting package metadata (current_repodata.json): done
Solving environment: done

# All requested packages already installed.

Installed mkl=2019 :

(base) (3.8.0/envs/my_virtual_env-3.8.0) marco@pc:~/facenet_pytorch/examples$ conda install  
mkl=2019
Collecting package metadata (current_repodata.json): done
Solving environment: done

# All requested packages already installed.

(base) (3.8.0/envs/my_virtual_env-3.8.0) marco@pc:~/facenet_pytorch/examples$ conda list | grep 
torch
cpuonly                   1.0                           0    pytorch
facenet-pytorch           0.1.0                    pypi_0    pypi
pytorch                   1.3.0               py3.7_cpu_0  [cpuonly]  pytorch
torchfile                 0.1.0                    pypi_0    pypi
torchvision               0.4.1                  py37_cpu  [cpuonly]  pytorch

But it still says "no module torch" :

(base) (3.8.0/envs/my_virtual_env-3.8.0) marco@pc:~/facenet_pytorch/examples$ python3
Python 3.8.0 (default, Oct 30 2019, 16:20:23) 
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'torch'
>>> 

Ubuntu 18.04.02 Server Edition

Any ideas to solve the problem?
Looking forward to your kind help.
Marco

Hi Marco, did you find any solution. i too have exact same issue with the same environment.

Hi @aks1087 I solved the problem with these two steps:
A) Used Python 3.7.3. since pytorch development guy told me that it's already supported for python 3.8.0
B) I ran again this command :
conda install pytorch torchvision cpuonly -c pytorch
since this line : cpuonly 1.0 0 pytorch
doesn't mean pytorch installed

Thanks @marcoippolito . Actually i wasnt using anaconda. so my pip was taking by default to 2.7 version of python. had to given 'python3.7 -m pip install....' to make it work. it just worked now. its confusing for the people new to linux

I had the same issue. Tried below in jupyter notebook in a conda env:
pip3 install torch torchvision

Exited out of the condo (base, as well) and Conda activates . It worked fine.

in official documentation it says u can install with pip and it doesn't work, u silly doughnuts, fix it for chrissake

I installed follow link: https://pytorch.org/. It works for me

Thanks, it worked for me

conda update --all solves the problem for me

After trying all the different solutions posted here (installed everything needed in both environments and still nothing) and some in stackoverflow, I ended up fixing it by doing a pip3 install.

My issue was that I was trying to run it in the new
"Python 3.7.6 64-bit" notebook when I switched to regular python3 it worked fine.

I followed these steps and it worked

conda install pytorch torchvision -c pytorch
pip3 install torch torchvision

For me, I have to change the kernel. I have 3 kernels to select in my jupyter and the one I using is not from conda environment.

To check which python is I use this code. It should have paths to conda's python.

import sys
print(sys.path)

same problem here. I installed pytorch but when i try to run it on text editor i get the "no module named torch". However, it does work in jupyter notebook. Any possible solution?

A combined solution of the above worked for me in Catalina:

conda update --all
conda install pytorch torchvision -c pytorch
pip3 install torch torchvision

all solution above didn't work :(

I think the problem is that jupyter is the global jupyter, not the jupyter in local environment.
So I try:
$activate (my env)
$(my env) pip install jupyter
and then
$(my env) jupyter notebook
It worked for me

"import torch" not working in jupyter notebook

OS: Ubuntu 18.04
Python version : 3.6.9

I had the same problem in conda. I have tried all solutions, none seem to be working. I have noticed that I had activated my pytorch environment from another conda environment. When I deactivate my pytorch, it goes back to second environment, not base. When I deactivated all, and re-activated pytorch it worked fine.

First you should have separate environment in Anaconda

conda install pytorch torchvision cpuonly -c pytorch

after run this installing getting error ModuleNotFoundError: No module named 'torch'

in the case of you need to install "conda install jupyter" after install notebook

import torch
not pytorch

In a system with multiple conda environments you need to install jupyter and torch again in that particular environment, otherwise conda can't import packages from other environments.

@nicospbr If it's still not resolved, run conda list and check whether jupyter is installed or not. If not, run conda install jupyter . Now, open jupyter notebook and run import torch .

I have jupyter installed in conda but still can't import torch

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Coderx7 picture Coderx7  ·  3Comments

NgPDat picture NgPDat  ·  3Comments

miguelvr picture miguelvr  ·  3Comments

eliabruni picture eliabruni  ·  3Comments

SeparateReality picture SeparateReality  ·  3Comments