I’m facing a strange issue given that suddenly, i can not anymore import torchvision.
I removed and installed pytorch + torchvision but it did not help.
>>> import torchvision
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/renaud/anaconda3/envs/tfe/lib/python3.6/site-packages/torchvision/__init__.py", line 3, in <module>
from torchvision import models
File "/home/renaud/anaconda3/envs/tfe/lib/python3.6/site-packages/torchvision/models/__init__.py", line 5, in <module>
from .inception import *
File "/home/renaud/anaconda3/envs/tfe/lib/python3.6/site-packages/torchvision/models/inception.py", line 6, in <module>
from torch.jit.annotations import Optional
ImportError: cannot import name 'Optional'
It seems that the installed PyTorch and torchvision versions are not compatible.
I guess that torchvision might be in a newer version and tries to call torch.jit.annotations.Optional, which isn’t available in your (older) PyTorch installation.
My torch version is 1.0.0 and in my conda env: 1.5.0
I can not print torchvision.__version__ given that I can not even import it, but the version in my conda env is 0.6.0.
As I mentionned, everything worked fine until this bug appeared. I always work in different conda env but is it possible that creating a new env with an older version of pytorch has produced this error ?
My torch version in python is 1.0.0 but in conda it is 1.5.0. How is it possible ? I guess the problem comes from my torch version if 1.0.0 is actually my torch version.
How did you check that torch.__version__ == 1.1.0 in your environment, while you are also able to see that you are using version 1.5.0?
Use the environment or terminal where you see the 1.5.0 output.
I’m not sure what’s causing the mismatch, but I would recommend to create a new conda env, make sure it’s clean and torch cannot be imported, and install the latest packages there.
No it does not work. Yesterday I went through the pytorch github to understand the dependencies and Optional comes from from typing import Tuple, List, Dict, Optional, Union, Any, TypeVar, Generic
If I import Optional without
from torch.jit.annotations import Optional
but with
from typing import Optional
This is working but then I have the error because of the following line in the inceptionv3 model
These failures all point to a torch version, which is too old, as both methods are relatively new.
I just reinstalled PyTorch 1.5.0 with torchvision0.6.0 and am able to import both libs as well as import torch.jit.annotations.Optional.
Could you check the path of torch via: print(torch.__path__) and make sure that it’s pointing to the right environment folder?
This is working when i’m in the folder in anaconda that contains torchvision.
But i don’t know how to change the sys.path in python such that it import my packages from anaconda/envs/....
By the way I also don’t know why it has changed.
Thank you for you help!
Do you have a hint how I can change the path used for loading those packages ?
I had the same issue and it solved with your answer, but each time I need to run the command in the terminal before I start the conda environment. how can fix this permanently