[resolved] Cannot import torch in python2

Hi there,

I have both python2 and python 3 installed and I also have pytorch and torch vision installed on both conda python2 and 3 environments using the conda environment. I previously trained a model in python3 using pytorch installed from anaconda.

I am now working in python2 and I need to import from a class from a module that was written in python3. When I do

import torch, I get ImportError: No module named torch. However, in my python3 environment, I can easily import torch. What could I be doing wrong?

Well, after many hours of trying I resolved to adding a conda env python alias to my bashrc file to distinguish the system python 2.7 from the conda one.

Something along the lines of alias py27='~/anaconda2/envs/py27/bin/python2'.

It is weird but before that, every time I called python, the system was executing the system wide python binary in /usr/bin.

Hope this helps someone.

1 Like