Pytorch listed by conda but cannot import

Viewed 603

I am well aware similar questions have been asked at least twice, but none of the answers seams to solve the issue at hand

My configuration

Windows 10.0.18363, Anaconda 4.8.5, Cuda 10.1.243

conda env create -n torch -y python 3.7
conda activate torch
conda install conda -y
conda install pytorch torchvision cudatoolkit=10.2 -c pytorch -y 

Here's the bug

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

What I tried

Verifying python and conda

where python
C:\ProgramData\Anaconda3\envs\torch\python.exe
C:\ProgramData\Anaconda3\python.exe
C:\csvn\Python25\python.exe
python -c "import site; print(site.getsitepackages())"

['C:\ProgramData\Anaconda3', 'C:\ProgramData\Anaconda3\lib\site-packages']

conda update -n base -y conda
conda update --all -y
conda init
...
No action taken.

Verifying the torch installation

conda list | findstr torch
_pytorch_select           0.1                       cpu_0
pytorch                   1.6.0           cpu_py37h538a6d7_0
torchvision               0.7.0                py37_cu102    pytorch
2 Answers

More a suggestion than a solution: you can at least reduce the problem surface by working with a YAML instead of using a series of create/activate/install commands. Create the file:

torch.yaml

name: torch
channels:
  - pytorch
  - defaults
dependencies:
  - python=3.7
  - pytorch
  - torchvision
  - cudatoolkit=10.2

Then simply use

conda env create -f torch.yaml

The result should be equivalent to the environment you indicated, covering both env creation and installation of all packages in a single command.1 Plus, you don't need all those pesky --yes|-y flags.

Any problems still persisting are most likely due to PATH or other environment variable management issues.


[1] I excluded conda from the YAML because that package should only be installed in base. Perhaps you meant anaconda package?

I tried out your answers, but none of them allowed me to load torch on my computer. The problem got solved with the new python 3.9. Seems it was a compatibility issue.

Related