I have installed torchvision in anaconda, but despite that, I am getting an import error when importing it into my code.
Here please note I have pytorch and torchvision installed on my system. But despite that, I am getting an import error while importing the module.
The code for importing is given below, which is flashing the error.
import torchvision.datasets as datasets
import torchvision.transform as transforms
Please help me out with this error. Please note I don’t have a GPU, so have only installed the cpu version of pytorch using,
conda install pytorch torchvision cpuonly -c pytorch
Please help me sort out this issue.
I have several ways for you to check the issue:
- Open your terminal, then type the
python -c "import sys; print(sys.path)"
- Check your site-package folder and make sure that datasets and transform folders are exists. (/home/gxuvimr/anaconda3/envs/pytorch/lib/python3.8/site-packages/torchvision
- Please make sure that your site-package folder is listed in the sys.path.
- Please also double check “_C.pyd” in the folder that I have just mentioned. Because in my past cases, import error is happening due to this file.
Hope this helps in your debugging, thanks~
Thanks for the prompt reply. I checked these packages and the above folder exists in my path.
Also, I do have _C.so in the folder above, but not _C.pyd.
Is it okay, or do I need to make any changes?
Could you try to run this:
python -c "import torchvision"
If you couldn’t, it means that there is a path problem inside your packages. You could check the other threads here:
Hope this helps, cheers~
You have a typo in your code.
import torchvision.transforms as transforms instead of
import torchvision.transform as transforms (note the additional s).
PS: it’s better to post code snippets by wrapping them into three backticks ```, as it makes debugging easier.
Starting in pytorch. Unable to find why there is a difference in behavior while doing import torchvision , in python vs ipython. I am getting the same module not found error in jupyter notebook even if the conda env installation was done correctly (using the command : conda install pytorch torchvision torchaudio cpuonly -c pytorch )
From the prompt, your
python is version 3.8 and your ipython is based on 3.7. So they just point to two different implementations of python. And it seems that you have torchvision installed for the python 3.8 but not for the 3.7.
You can try to remove extra python installs to avoid such issues. Or use virtualenv that will make sure to have all of these consistent.
thanks! I had the same problem and this solved it
I was quite sceptic though about “transform”