Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModuleNotFoundError: No module named 'torch' #4827

Closed
xiaonengmiao opened this issue Jan 24, 2018 · 78 comments
Closed

ModuleNotFoundError: No module named 'torch' #4827

xiaonengmiao opened this issue Jan 24, 2018 · 78 comments

Comments

@xiaonengmiao
Copy link

  • 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

@vishwakftw
Copy link
Contributor

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

@xiaonengmiao
Copy link
Author

@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

@xiaonengmiao
Copy link
Author

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

@Jerrybonheur
Copy link

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

@xiaonengmiao
Copy link
Author

@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

@nicospbr
Copy link

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

Any ideas?

@jithinjk
Copy link

jithinjk commented Mar 6, 2018

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.

@ghost
Copy link

ghost commented Apr 16, 2018

@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 .

@YakshThakar
Copy link

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

@romanovacca
Copy link

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

@atan4583
Copy link

atan4583 commented Jul 7, 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.

@jleni
Copy link

jleni commented Jul 17, 2018

@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

@soumith
Copy link
Member

soumith commented Jul 17, 2018

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.

@jleni
Copy link

jleni commented Jul 17, 2018

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.

@jleni
Copy link

jleni commented Jul 17, 2018

mm.. It seems that I hit this issue here: jupyterlab/jupyterlab#4672

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

@imflash217
Copy link

@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.

@erseler
Copy link

erseler commented Oct 4, 2018

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

@ervin-x
Copy link

ervin-x commented Oct 6, 2018

The command to import pytorch is:

import torch
not
import pytorch

@JimLiu0327
Copy link

      The command to import pytorch is:

import torch
not
import pytorch

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

@caothuong
Copy link

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

@lal1234
Copy link

lal1234 commented Feb 14, 2019

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

@houhaol
Copy link

houhaol commented Mar 11, 2019

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

@houhaol
Copy link

houhaol commented Mar 11, 2019

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

@MxGutierrez
Copy link

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?

@lamnguyenx
Copy link

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?

@houhaol
Copy link

houhaol commented Mar 15, 2019

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.

@lamnguyenx
Copy link

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.

@ChiaYuWu
Copy link

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.

@saraXX
Copy link

saraXX commented Jun 12, 2020

all solution above didn't work :(

@urayoru113
Copy link

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

@sreshu
Copy link

sreshu commented Aug 7, 2020

"import torch" not working in jupyter notebook

OS: Ubuntu 18.04
Python version : 3.6.9

@obbrc
Copy link

obbrc commented Aug 11, 2020

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.

@kanhataak
Copy link

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

@DebadityaShome
Copy link

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.

@bakaito
Copy link

bakaito commented Sep 23, 2020

@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

@fathia-ghribi
Copy link

when i run import torch , an error occurs ModuleNotFoundError: No module named 'torch'.

@DebadityaShome
Copy link

@fathia-ghribi Can you give more details which OS / Environments are you using?

@bakaito
Copy link

bakaito commented Dec 3, 2020 via email

@fathia-ghribi
Copy link

@ fathia-ghribi Pouvez-vous donner plus de détails sur le système d'exploitation / l'environnement que vous utilisez?
My OS : Windos 10 , i use anaconda and jupyter notebook

@fathia-ghribi
Copy link

S'il s'agit d'un notebook jupyter et que vous avez installé pytorch dans un environnement différent de celui par défaut, assurez-vous d'activer celui où pytorch est installé (conda activate <env_name>)

Le jeu 3 décembre 2020 à 10 h 02 Debaditya Shome @.***> a écrit: @ fathia-ghribi < https://github.com/fathia-ghribi > Pouvez-vous donner plus de détails quel système d'exploitation / environnement utilisez-vous? - Vous recevez ceci parce que vous avez commenté. Répondez directement à cet e-mail, affichez-le sur GitHub < # 4827 (commentaire) > ou désabonnez-vous < https://github.com/notifications/unsubscribe-auth/AOR62PXXQ2M3YKMK3VBQ2PLSS4IFRANCNFSM4ENLF4XQ >.

I created an environment named 'test', after I executed the command: conda activate test, when I run import torch,an error occurs ModuleNotFoundError: No module named 'torch'.

@bakaito
Copy link

bakaito commented Dec 3, 2020 via email

@bakaito
Copy link

bakaito commented Dec 3, 2020 via email

@fathia-ghribi
Copy link

i tried with (https://pytorch.org/get-started/locally/)
but
image

@fathia-ghribi
Copy link

hi all,
no one can help me to solve this error.
image

@hjw21century
Copy link

First, you should get which jupyter you are using by 'which jupyter' command.
Then, you get which python interpreter you are using by 'vim /usr/bin/jupyter' for example.
If the python version is not what you expected. now reinstall jupyter binded with the python version you expect.

@hjw21century
Copy link

Also, you can directly check the jupyter kernel setup in '/usr/local/share/jupyter/kernels/python3/kernel.json' file.

@priyarana
Copy link

all solution above didn't work :(

Nothing is working for me. Did u solve the problem finally? thanks

@ankitajaiswal00
Copy link

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!.

@Anhar2
Copy link

Anhar2 commented Jul 8, 2021

Installing pytorch in the base and using the base kernel "i.e Python3" in jupyter notebook only worked fine, using any other kernel with pytorch installed never worked

@bralat
Copy link

bralat commented Jul 20, 2021

I use WSL and what worked for me was installing mkl=2018 as suggested in previous answers,

conda install mkl=2018

then closing my windows terminal and reopening it.

@jaydeepsb
Copy link

Solved!
I had similar problem. I created conda env called torch.
Within command line ipython, I could import torch successfully. But when I tried to import torch inside jupyter notebook it failed.
The problem was due to the way I registered my new env kernel called torch. I was in a different (wrong) env when I ran the following command.
python -m install ipykernel --user --name=torch --display_name='torch

This I realized by printing import sys; sys.path in jupyter notebook. It was pointing to different site-packages folder.

Correct solution is -

conda activate your_torch_env
conda install ipykernel #if you have not installed already
ipython kernel install --user --name=your_torch_env

@AnjaniSonavane
Copy link

conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch

@YUyttendaele
Copy link

What worked for me was reinstalling jupyter notebook:

conda install jupyter notebook

@w3nhao
Copy link

w3nhao commented Apr 28, 2022

$ conda activate your_torch_env  
$ conda install ipykernel #if you have not installed already  
$ ipython kernel install --user --name=your_torch_env  

Try this first, if it doesn't work, you can add the path of your env' python manually to the kernelspec json file.

Find your env' python' path by:

$ whereis python

Copy the path and manually put it into the kernelspec newly generated:

$ vim /home/your_user_name/.local/share/jupyter/kernels/your_torch_env/kernel.json

@lexson-luo
Copy link

This Pytorch site helped:

https://github.com/pytorch/pytorch#from-source

in cmd prompt cd to your Lib/site-packages, then input below:

git clone --recursive https://github.com/pytorch/pytorch

@jiwan-swfit
Copy link

@Jerrybonheur conda가 있는 경우 conda를 사용하여 pytorch를 설치해 보십시오(예: Mac).

$conda 설치 pytorch torchvision -c pytorch

이것이 여전히 작동하지 않으면 다음을 시도하십시오.

$conda 업데이트 conda $conda 설치 mkl=2018

감사합니다. 해결 됬어요

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests