ImportError: cannot import name 'Optional'

Hello,

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'

Can someone help me with this ?

2 Likes

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.

Could you check the versions of both libs via:

print(torch.__version__)
print(torchvision.__version__)

And make sure to use the latest stable release or the nightly binaries for both?

2 Likes

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 ?

If so what is the best way to recover from that ?

Thank you

PyTorch 1.5.0 and torchvision 0.6.0 should be compatible.
Are you using the 1.0.0 version by any chance?

Yeah, you are right. That didn’t make sense to suggest. :slight_smile:

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.

The system Python will use it’s own “system environment” (or a pip virtual env), while conda uses its base env or any other which you’ve created.

You would have to make sure to use the PyTorch installed in conda e.g. by activating the environment it was installed in.

If you don’t need the legacy 1.0.0 installation, you could of course also remove it.

Yes my pytorch=1.5.0 and torchvision=0.6.0 have been installed in my coda env using conda install ....

How can I solve the fact that even in my environment, my torch.__version__ is 1.0.0 ?

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.

Here are some screenshots:


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.

Just created a new conda env, python=3.6.9 and just installed on it

conda install pytorch torchvision cpuonly -c pytorch

Same error as i try to import torchvision

Are you able to from torch.jit.annotations import Optional without trying to import torchvision?

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 torchvision 0.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?

It looks like the issue is there as my path does not include “anaconda”.

>>> import torch
>>> torch.__path__
['/home/renaud/.local/lib/python3.6/site-packages/torch']

even though the command which python includes the path of my conda env.

Could you run Python by specifying the complete conda path and try to import torchvision again?

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 ?

Solved the problem of path with

export PYTHONNOUSERSITE=True
2 Likes

Thanks for posting the solution!
I haven’t seen this path mismatch before in this manner, but I think it’ll be useful for others. :slight_smile:

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