3

Last week I had a working conda env I was using for a project. I have not touched the project in a week. I just went to run a python file (python file.py) that had been running with no errors.

Now I get the following error:

Traceback (most recent call last):
  File "file.py", line 2, in <module>
    from torch.utils.data import Dataset, DataLoader
ModuleNotFoundError: No module named 'torch'

In an attempt to troubleshoot, I opened a python console and ran the following code:

>>> import torch

The result was the following error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'torch'

If I check all the installed packages using conda list -n <env_name>, I can see that PyTorch is in fact installed, just as it was last week.

...
pytorch                   1.2.0           py3.7_cuda9.2.148_cudnn7.6.2_0    pytorch
...
torchvision               0.4.0                 py37_cu92    pytorch
...

Here is what I see when I start python console using python:

Python 3.7.4 (default, Aug 13 2019, 20:35:49) 
[GCC 7.3.0] :: Anaconda, Inc. on linux

The output of python -c 'import sys; print(sys.path) in the base env is:

['', '/home/<name>/anaconda3/lib/python37.zip', '/home/<name>/anaconda3/lib/python3.7', '/home/<name>/anaconda3/lib/python3.7/lib-dynload', '/home/<name>/anaconda3/lib/python3.7/site-packages'] I have not personally made any chages to PYTHONPATH.

If I run python -c 'import sys; print(sys.path)' with my conda env (non-base) active, I get:

['', '/home/<name>/anaconda3/envs/<env_name>/lib/python37.zip', '/home/<name>/anaconda3/envs/<env_name>/lib/python3.7', '/home/<name>/anaconda3/envs/<env_name>/lib/python3.7/lib-dynload', '/home/<name>/anaconda3/envs/<env_name>/lib/python3.7/site-packages']

This is totally bizarre, can't figure out what is going on, and what could have happened, over the course of the last week, without me touching the code or making any changes to Anaconda.

HMLDude
  • 1,547
  • 7
  • 27
  • 47
  • 1
    Are you sure you're not in the base env, ie did you activate the environment? – it's-yer-boy-chet Sep 24 '19 at 16:27
  • 1
    Yes, I actually have three environments that have both pytorch and torchvision installed, none of them are working properly. – HMLDude Sep 24 '19 at 16:30
  • 1
    Do you have an `environment.yml` file that you can compare to the output of `conda env export > environment.yml` to see what changes there might be? – it's-yer-boy-chet Sep 24 '19 at 16:38
  • 1
    @merv I ran the command you suggested from both from my custom env and from the base conda env and added the output to the question. – HMLDude Sep 27 '19 at 16:03
  • 1
    @HMLDude thanks for adding that. That looks correct now, though. Before you didn't have `env_name` in the `sys.path`. Is it working now? – merv Sep 27 '19 at 16:09
  • 2
    Yes, but only after taking the steps listed below. I am having a hard time understanding how everything was working a couple of weeks ago, and then it stopped working this week, without me making any changes in between. – HMLDude Sep 27 '19 at 18:51

1 Answers1

3
  1. open anaconda-prompt then run this

    conda install PyTorch -c PyTorch

  2. If you didn't upgrade your pip.use this command to update

    python -m pip install –upgrade pip

  3. After first step run this

    pip3 install torchvision

hope it will work.

Kalana
  • 5,631
  • 7
  • 30
  • 51
  • 1
    The only thing that did not work was the second command you suggested. Instead I did `pip install --upgrade pip`. Otherwise, it all worked, thanks much! BTW, all these commands were executed in the specific conda env that was giving me trouble. – HMLDude Sep 24 '19 at 21:26
  • 1
    I got the same issue that you got. There for I was able to answer your question.Happy coding @HMLDude – Kalana Sep 25 '19 at 04:36