4

I am trying to import torchvision but I am getting an error. I researched the topic in the Web and there seems to be a problem with the later versions. Different solutions were offered --mostly reinstalling the modules-- which I tried without a result The latest advice I found was to install torch and torchvision using the following command which I did successfully:

pip install torch===1.4.0 torchvision===0.5.0 -f https://download.pytorch.org/whl/torch_stable.html

Still, the error persists:

(tf2) C:\Users\Alienware>python
Python 3.7.6 | packaged by conda-forge | (default, Mar 23 2020, 22:22:21) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> import torchvision
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Alienware\Anaconda3\envs\tf2\lib\site-packages\torchvision\__init__.py", line 3, in <module>
    from torchvision import models
  File "C:\Users\Alienware\Anaconda3\envs\tf2\lib\site-packages\torchvision\models\__init__.py", line 5, in <module>
    from .inception import *
  File "C:\Users\Alienware\Anaconda3\envs\tf2\lib\site-packages\torchvision\models\inception.py", line 65, in <module>
    class Inception3(nn.Module):
  File "C:\Users\Alienware\Anaconda3\envs\tf2\lib\site-packages\torchvision\models\inception.py", line 182, in Inception3
    @torch.jit.unused
AttributeError: module 'torch.jit' has no attribute 'unused'

I am running Windows10, Anaconda 3, Python 3.7. My PC has a GPU and I have installed CUDA which works with TensorFlow and PyTorch.

4 Answers 4

7

For me downgrading torchvision to 0.4.0 did the trick:

pip uninstall torchvision
pip install torchvision==0.4.0 -f https://download.pytorch.org/whl/torch_stable.html
1

I was able to import torchvision in an Environment ruining Python 3.6. It was impossible to run it with Python 3.7.

1

tl;dr pip3 install torchvision to upgrade torch and torchvision to latest compatible versions.

torchvision 0.7 has been released and is compatible with torch 1.6. It does not appear to be compatible with torch 1.5.

Note that installing torchvision via PyCharm may not result in an automatic upgrade of torch. Installing via pip3 did automatically upgrade torch however.

1
  • For me, torch was 1.4.0. I only used the command. It uninstalled torch-1.4.0 and installed torch-1.12.0 automatically. Then, it worked.
    – Redhwan
    Commented Feb 13, 2023 at 1:33
0

I am able to get this to run with the following commands:

conda create --name test5 python=3.6
conda install -c pytorch pytorch torchvision cpuonly

python
>>> import torch
>>> import torchvision
1
  • I believe the results of the install command will vary over time (different versions may be used). Conda chooses appropriate versions for you, so this may work for some users, but will not necessarily for others. Commented May 7, 2022 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.