No module named "Torch"
Question:
I successfully installed pytorch via conda:
conda install pytorch-cpu torchvision-cpu -c pytorch
I also successfully installed pytorch via pip:
pip3 install https://download.pytorch.org/whl/cpu/torch-1.0.1-cp36-cp36m-win_amd64.whl
pip3 install torchvision
But, it only works in a jupyter notebook. Whenever I try to execute a script from the console, I get the error message:
No module named "torch"
Answers:
Welcome to SO,
please create a seperate conda environment
activate this environment conda activate myenv
and than install pytorch in it.
Besides you can check which python you are currently using by which python
You need to add this at the very top of your program
import torch
If this is not a problem execute this program on both Jupiter and command line and pretty much you will understand if you have a mismatch.
import sys
print(sys.executable)
Try to install PyTorch using pip:
First create a Conda environment using:
conda create -n env_pytorch python=3.6
Activate the environment using:
conda activate env_pytorch
Now install PyTorch using pip:
pip install torchvision
Note: This will install both torch and torchvision.
Now go to Python shell and import using the command:
import torch
import torchvision
Usually if the torch/tensorflow has been successfully installed, you still cannot import those libraries, the reason is that the python environment you try to import is not the python environment you installed.
For example, if you have installed the torch/tensorflow using python='/usr/bin/python'
, then you cannot import them to python='/home/usrname/.../bin/python'
.
The solution is simple thus, just change the python and import again.
I installed on my macos by the official command:
conda install pytorch torchvision -c pytorch
but when I follow the official verification I get the same problem like yours.
Then I create a conda virtual environment:
conda create --name learnpytorch python=3.5
and install pytorch inside the environment:
conda install pytorch torchvision -c pytorch
run the verification, it works.
Hope these could help you.
I am struggling the above answer and it does not work in my case.
That is because I install the porch under window COMMAND PROMPT.
- Installed under the Anaconda environment command prompt. To do that, type “anaconda” and select the ANACONDA COMMAND PROMPT (this is very important for me to fix my mistake)
- Do the same command from: https://pytorch.org/get-started/locally/
Create the environment:
conda create -n env_pytorch python=3.6
Install your modules. For example:
conda install pytorch torchvision cudatoolkit=10.2 -c pytorch
Next time you want to use pytorch:
conda activate env_pytorch
and when finished:
conda deactivate
Make sure that NumPy and Scipy libraries are installed before installing the torch library that worked for me at least on windows.
- Install NumPy:
pip install numpy
- Install Scipy:
pip install scipy
- Go to pytorch.org and select your needs and copy the address
- Paste the address and download
Check SDK install. you also install the sdk in your environment.
You can install the SDK using navigator.
first, change tab to Environments,
second, push play button of your environment name.
Third, then install SDK.
Switch to python3
on the notebook
I had the same problem after following the official installation guide in here. I realized that it was my stupid Atom built-in terminal.
Inside the atom terminal, which python
returned /usr/bin/python, although it showed the mark of (conda_env)$.
I fixed it by opening a new terminal and activating the (conda_env), and it worked. Verified and which python
yields /Users/my_usr/anaconda3/envs/conda_env/bin/python
If you are using Anaconda Prompt, there is a simpler way to solve this.
conda install -c pytorch pytorch
If you are using windows and you have no CUDA, then go with the following command.
pip install torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio===0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
You can visit its official site for more explanation.
Pytorch Official site
If you are in the console, and importing a function that uses torch, you may need to add import torch
within the function to allow for the correct scope. Because if you are importing the function, and there is no import statement at the top of the file, it won’t work. Alternatively, make sure import torch
is at the top of the module with the function you are trying to use, and within console, call the function using: your_module.function_that_references_torch()
The procedure I used is specific to Windows 10 PyTorch
installation on anaconda
.
- Create a conda virtual environment using:
conda create -n torch_env
- Activate virtual environment using:
conda activate torch_env
- When I installed, this was my current config:
conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
.
Please find correct configuration according to your system settings from pytorch website directly: https://pytorch.org/get-started/locally/
Run python
with import torch
command. It should definitely work!
Using Conda on win 10 running script from vs code terminal as:
$ script.py
Generates error:
ModuleNotFoundError: No module named 'torch'
The system goes outside the environment to execute call python.
This works:
$ python script.py
I tried to fixed the problem both in my laptop and computer, and it was fixed in different ways. You can try.
Laptop(Nvidia GTX 950M)
I fixed the problem by typing this under the environment that you install pytorch.
$ conda install pytorch torchvision cudatoolkit=10.0 -c pytorch
Remember to change the version(10.0) of you cuda
Computer(No GPU)
$ conda update -n base -c defaults conda
If you are using conda, it’s possible that your console is using a different version of python, and the packages you installed using conda are not installed on this version. In my case, I installed a 3.9 version by downloading from the website, and conda installed a 3.8 version. And all the packages only installed for 3.8. Deleting the 3.9 version did the trick for me.
I had the same problem right after installing pytorch from the console, without closing it and restarting it.
By restarting the console and re-entering my env, I solved the problem
Most of the answers to this question are unsatisfying in that they explain how to create and activate a conda environment. The reason for the error is however not explained.
In my case, I had a conda environment set up, but the torch module was still not found, even if I installed it.
The reason for the error is that python v2 was the main interpreter, not python3.
You can test that by running python --version
Then follow the instructions from above to install the conda environment with the correct python version, e.g.
conda create -n venv python=3.9
I had an issue related to the path. Basically if I ran python
with the path from which python
, import torch
works, if I just run python
, it doesn’t work.
This solution from @shoemakerdr on GitHub worked for me:
In your .bashrc
, before the Conda initialize code, put unset CONDA_SHLVL
I’m using Jupyter Notebook launching from Anaconda Navigator 2.3.2 (Windows 10) to investigate pyTorch in a new Environment created in Navigator. Before launching I added pyTorch via a Command Prompt with the new Environment activated using the following which I got from pytorch.org:
conda install pytorch torchvision torchaudio cpuonly -c pytorch
I then ran into the No module named "torch" issue and spent many hours looking into this. I was eventually able to fix this issue looking at the results of this:
import sys
print(sys.path)
For me, this showed that the path to site-packages for my kernal (aka Environment) was missing. So I edited the kernal’s kernal.json file found in:
<DRIVE_LETTER>:Users<USER_NAME>AppDataRoamingjupyterkernels<KERNAL_NAME>
To include an env
section, which adds to PYTHONPATH
like:
{
"argv": [
"<ANACONDA_INSTALL_DIR>\envs\<KERNAL_NAME>\python.exe",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"env": {
"PYTHONPATH": "..\..\..\Users\<USER_NAME>\<ANACONDA_INSTALL_DIR_NAME>\envs\<KERNAL_NAME>\Lib\site-packages"
},
"display_name": "<KERNAL_NAME>",
"language": "python",
"metadata": {
"debugger": true
}
}
Please note that in my case I had to go back up 3 directories to the root of the drive, hence, the ..\..\..\
at the start of the PYTHONPATH
entry. You might need to change that. Also if you try to use a full path, to your Environment’s site-packages, then it will prepend the current contents of PYTHONPATH
to it instead of adding it as a separate entity.
I successfully installed pytorch via conda:
conda install pytorch-cpu torchvision-cpu -c pytorch
I also successfully installed pytorch via pip:
pip3 install https://download.pytorch.org/whl/cpu/torch-1.0.1-cp36-cp36m-win_amd64.whl
pip3 install torchvision
But, it only works in a jupyter notebook. Whenever I try to execute a script from the console, I get the error message:
No module named "torch"
Welcome to SO,
please create a seperate conda environment
activate this environment conda activate myenv
and than install pytorch in it.
Besides you can check which python you are currently using by which python
You need to add this at the very top of your program
import torch
If this is not a problem execute this program on both Jupiter and command line and pretty much you will understand if you have a mismatch.
import sys
print(sys.executable)
Try to install PyTorch using pip:
First create a Conda environment using:
conda create -n env_pytorch python=3.6
Activate the environment using:
conda activate env_pytorch
Now install PyTorch using pip:
pip install torchvision
Note: This will install both torch and torchvision.
Now go to Python shell and import using the command:
import torch
import torchvision
Usually if the torch/tensorflow has been successfully installed, you still cannot import those libraries, the reason is that the python environment you try to import is not the python environment you installed.
For example, if you have installed the torch/tensorflow using python='/usr/bin/python'
, then you cannot import them to python='/home/usrname/.../bin/python'
.
The solution is simple thus, just change the python and import again.
I installed on my macos by the official command:
conda install pytorch torchvision -c pytorch
but when I follow the official verification I get the same problem like yours.
Then I create a conda virtual environment:
conda create --name learnpytorch python=3.5
and install pytorch inside the environment:
conda install pytorch torchvision -c pytorch
run the verification, it works.
Hope these could help you.
I am struggling the above answer and it does not work in my case.
That is because I install the porch under window COMMAND PROMPT.
- Installed under the Anaconda environment command prompt. To do that, type “anaconda” and select the ANACONDA COMMAND PROMPT (this is very important for me to fix my mistake)
- Do the same command from: https://pytorch.org/get-started/locally/
Create the environment:
conda create -n env_pytorch python=3.6
Install your modules. For example:
conda install pytorch torchvision cudatoolkit=10.2 -c pytorch
Next time you want to use pytorch:
conda activate env_pytorch
and when finished:
conda deactivate
Make sure that NumPy and Scipy libraries are installed before installing the torch library that worked for me at least on windows.
- Install NumPy:
pip install numpy
- Install Scipy:
pip install scipy
- Go to pytorch.org and select your needs and copy the address
- Paste the address and download
Check SDK install. you also install the sdk in your environment.
You can install the SDK using navigator.
first, change tab to Environments,
second, push play button of your environment name.
Third, then install SDK.
Switch to python3
on the notebook
I had the same problem after following the official installation guide in here. I realized that it was my stupid Atom built-in terminal.
Inside the atom terminal, which python
returned /usr/bin/python, although it showed the mark of (conda_env)$.
I fixed it by opening a new terminal and activating the (conda_env), and it worked. Verified and which python
yields /Users/my_usr/anaconda3/envs/conda_env/bin/python
If you are using Anaconda Prompt, there is a simpler way to solve this.
conda install -c pytorch pytorch
If you are using windows and you have no CUDA, then go with the following command.
pip install torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio===0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
You can visit its official site for more explanation.
Pytorch Official site
If you are in the console, and importing a function that uses torch, you may need to add import torch
within the function to allow for the correct scope. Because if you are importing the function, and there is no import statement at the top of the file, it won’t work. Alternatively, make sure import torch
is at the top of the module with the function you are trying to use, and within console, call the function using: your_module.function_that_references_torch()
The procedure I used is specific to Windows 10 PyTorch
installation on anaconda
.
- Create a conda virtual environment using:
conda create -n torch_env
- Activate virtual environment using:
conda activate torch_env
- When I installed, this was my current config:
conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
.
Please find correct configuration according to your system settings from pytorch website directly: https://pytorch.org/get-started/locally/
Run python
with import torch
command. It should definitely work!
Using Conda on win 10 running script from vs code terminal as:
$ script.py
Generates error:
ModuleNotFoundError: No module named 'torch'
The system goes outside the environment to execute call python.
This works:
$ python script.py
I tried to fixed the problem both in my laptop and computer, and it was fixed in different ways. You can try.
Laptop(Nvidia GTX 950M)
I fixed the problem by typing this under the environment that you install pytorch.
$ conda install pytorch torchvision cudatoolkit=10.0 -c pytorch
Remember to change the version(10.0) of you cuda
Computer(No GPU)
$ conda update -n base -c defaults conda
If you are using conda, it’s possible that your console is using a different version of python, and the packages you installed using conda are not installed on this version. In my case, I installed a 3.9 version by downloading from the website, and conda installed a 3.8 version. And all the packages only installed for 3.8. Deleting the 3.9 version did the trick for me.
I had the same problem right after installing pytorch from the console, without closing it and restarting it.
By restarting the console and re-entering my env, I solved the problem
Most of the answers to this question are unsatisfying in that they explain how to create and activate a conda environment. The reason for the error is however not explained.
In my case, I had a conda environment set up, but the torch module was still not found, even if I installed it.
The reason for the error is that python v2 was the main interpreter, not python3.
You can test that by running python --version
Then follow the instructions from above to install the conda environment with the correct python version, e.g.
conda create -n venv python=3.9
I had an issue related to the path. Basically if I ran python
with the path from which python
, import torch
works, if I just run python
, it doesn’t work.
This solution from @shoemakerdr on GitHub worked for me:
In your .bashrc
, before the Conda initialize code, put unset CONDA_SHLVL
I’m using Jupyter Notebook launching from Anaconda Navigator 2.3.2 (Windows 10) to investigate pyTorch in a new Environment created in Navigator. Before launching I added pyTorch via a Command Prompt with the new Environment activated using the following which I got from pytorch.org:
conda install pytorch torchvision torchaudio cpuonly -c pytorch
I then ran into the No module named "torch" issue and spent many hours looking into this. I was eventually able to fix this issue looking at the results of this:
import sys
print(sys.path)
For me, this showed that the path to site-packages for my kernal (aka Environment) was missing. So I edited the kernal’s kernal.json file found in:
<DRIVE_LETTER>:Users<USER_NAME>AppDataRoamingjupyterkernels<KERNAL_NAME>
To include an env
section, which adds to PYTHONPATH
like:
{
"argv": [
"<ANACONDA_INSTALL_DIR>\envs\<KERNAL_NAME>\python.exe",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"env": {
"PYTHONPATH": "..\..\..\Users\<USER_NAME>\<ANACONDA_INSTALL_DIR_NAME>\envs\<KERNAL_NAME>\Lib\site-packages"
},
"display_name": "<KERNAL_NAME>",
"language": "python",
"metadata": {
"debugger": true
}
}
Please note that in my case I had to go back up 3 directories to the root of the drive, hence, the ..\..\..\
at the start of the PYTHONPATH
entry. You might need to change that. Also if you try to use a full path, to your Environment’s site-packages, then it will prepend the current contents of PYTHONPATH
to it instead of adding it as a separate entity.