"no module named torch". But installed pytorch 1.3.0 with conda in Ubuntu 18.04.02 Server Edition

14,091

Solution 1

First create a Conda environment using:

conda create -n pytorch_env python=3 ( you can create with any python version )

Activate the environment using:

conda activate pytorch_env

Now install PyTorch using:

conda install pytorch-cpu torchvision -c pytorch

Go to python shell and import using the command:

import torch

Solution 2

Pytorch can be installed via pip and conda. For that, you need to create a separate conda environment. Thus, it will not corrupt the base environment. Steps to create a new conda environment as follows:

conda create -n conda_pytorch python=3.6
source activate conda_pytorch 

Follow the below command to install pytorch via pip:

pip install torch==1.3.1+cpu torchvision==0.4.2+cpu -f https://download.pytorch.org/whl/torch_stable.html 

Pytorch installation via conda:

conda install pytorch torchvision cpuonly -c pytorch

Verify the pytorch installation in the python shell using:

import torch

Solution 3

Thanks all for your kind answers. I solved the problem - first, "downgrading" python from 3.8.0 to 3.7.3 because I checked in PyTorch's chat environment that PyTorch is not yet compatible with python 3.8.0 - and then, after removing everything already installed, installing the latest version of PyTorch via cunda, as you kindly explained

Share:
14,091
user2315094
Author by

user2315094

Updated on June 05, 2022

Comments

  • user2315094
    user2315094 almost 2 years

    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'
    >>> 
    

    I discovered that the problem appears only with python 3.8.0 version

    (base) marco@pc:~/facenet_pytorch$ python3
    Python 3.7.3 (default, Mar 27 2019, 22:11:17) 
    [GCC 7.3.0] :: Anaconda, Inc. on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import torch
    >>> 
    

    Ubuntu 18.04.02 Server Edition

    Or, may be, it's just a matter of python environments, as you said. But I do not understand why just activating conda environment, with "conda activate", it doesn't work

    Marco