ImportError: cannot import name 'Optional'

rvandeghen: <p>Hello,</p>
<p>I’m facing a strange issue given that suddenly, i can not anymore import torchvision.<br>
I removed and installed pytorch + torchvision but it did not help.</p>
<pre><code class="lang-auto">&gt;&gt;&gt; import torchvision
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/home/renaud/anaconda3/envs/tfe/lib/python3.6/site-packages/torchvision/__init__.py", line 3, in &lt;module&gt;
    from torchvision import models
  File "/home/renaud/anaconda3/envs/tfe/lib/python3.6/site-packages/torchvision/models/__init__.py", line 5, in &lt;module&gt;
    from .inception import *
  File "/home/renaud/anaconda3/envs/tfe/lib/python3.6/site-packages/torchvision/models/inception.py", line 6, in &lt;module&gt;
    from torch.jit.annotations import Optional
ImportError: cannot import name 'Optional'
</code></pre>
<p>Can someone help me with this ?</p>

ptrblck: <p>It seems that the installed PyTorch and <code>torchvision</code> versions are not compatible.<br>
I guess that <code>torchvision</code> might be in a newer version and tries to call <code>torch.jit.annotations.Optional</code>, which isn’t available in your (older) PyTorch installation.</p>
<p>Could you check the versions of both libs via:</p>
<pre><code class="lang-python">print(torch.__version__)
print(torchvision.__version__)
</code></pre>
<p>And make sure to use the latest stable release or the nightly binaries for both?</p>

rvandeghen: <p>My torch version is 1.0.0 and in my conda env: 1.5.0<br>
I can not print <code>torchvision.__version__</code> given that I can not even import it, but the version in my conda env is 0.6.0.</p>
<p>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 ?</p>
<p>If so what is the best way to recover from that ?</p>
<p>Thank you</p>

ptrblck: <p>PyTorch <code>1.5.0</code> and torchvision <code>0.6.0</code> should be compatible.<br>
Are you using the <code>1.0.0</code> version by any chance?</p>
<aside class="quote no-group" data-username="rvandeghen" data-post="3" data-topic="80007">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/rvandeghen/48/23881_2.png" class="avatar"> rvandeghen:</div>
<blockquote>
<p>I can not print <code>torchvision.__version__</code> given that I can not even import it, but the version in my conda env is 0.6.0.</p>
</blockquote>
</aside>
<p>Yeah, you are right. That didn’t make sense to suggest. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

rvandeghen: <aside class="quote no-group" data-username="ptrblck" data-post="4" data-topic="80007">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>PyTorch <code>1.5.0</code> and torchvision <code>0.6.0</code> should be compatible.<br>
Are you using the <code>1.0.0</code> version by any chance?</p>
</blockquote>
</aside>
<p>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.</p>

ptrblck: <p>The system Python will use it’s own “system environment” (or a pip virtual env), while <code>conda</code> uses its <code>base</code> env or any other which you’ve created.</p>
<p>You would have to make sure to use the PyTorch installed in conda e.g. by activating the environment it was installed in.</p>
<p>If you don’t need the legacy <code>1.0.0</code> installation, you could of course also remove it.</p>

rvandeghen: <p>Yes my pytorch=1.5.0 and torchvision=0.6.0 have been installed in my coda env using <code>conda install ...</code>.</p>
<p>How can I solve the fact that even in my environment, my <code>torch.__version__</code> is 1.0.0 ?</p>

ptrblck: <p>How did you check that <code>torch.__version__ == 1.1.0</code> in your environment, while you are also able to see that you are using version <code>1.5.0</code>?<br>
Use the environment or terminal where you see the <code>1.5.0</code> output.</p>

rvandeghen: <p>Here are some screenshots:<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/a/2a2ca0c1a7b57b00c0e8285d40cc48b8a1b1af5a.png" data-download-href="https://discuss.pytorch.org/uploads/default/2a2ca0c1a7b57b00c0e8285d40cc48b8a1b1af5a" title="torch"><img src="https://discuss.pytorch.org/uploads/default/original/3X/2/a/2a2ca0c1a7b57b00c0e8285d40cc48b8a1b1af5a.png" alt="torch" data-base62-sha1="615FswDDJXE8roHIhfzpRId8OMy" width="690" height="183" data-dominant-color="181A1A"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">torch</span><span class="informations">859×229 8.91 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/4/045ed2f5132dd296dfcb782339bb1b5817856a69.png" data-download-href="https://discuss.pytorch.org/uploads/default/045ed2f5132dd296dfcb782339bb1b5817856a69" title="vision"><img src="https://discuss.pytorch.org/uploads/default/original/3X/0/4/045ed2f5132dd296dfcb782339bb1b5817856a69.png" alt="vision" data-base62-sha1="CF4j49NB37l8jK7da2eiY68bS9" width="690" height="286" data-dominant-color="161616"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">vision</span><span class="informations">864×359 10.6 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>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 <code>torch</code> cannot be imported, and install the latest packages there.</p>

rvandeghen: <p>Just created a new conda env, python=3.6.9 and just installed on it</p>
<pre><code class="lang-auto">conda install pytorch torchvision cpuonly -c pytorch

</code></pre>
<p>Same error as i try to import torchvision</p>

ptrblck: <p>Are you able to <code>from torch.jit.annotations import Optional</code> without trying to import <code>torchvision</code>?</p>

rvandeghen: <p>No it does not work. Yesterday I went through the pytorch github to understand the dependencies and <code>Optional</code> comes from<br>
<code>from typing import Tuple, List, Dict, Optional, Union, Any, TypeVar, Generic</code></p>
<p>If I import Optional without</p>
<pre><code class="lang-auto">from torch.jit.annotations import Optional
</code></pre>
<p>but with</p>
<pre><code class="lang-auto">from typing import Optional
</code></pre>
<p>This is working but then I have the error because of the following line in the inceptionv3 model</p>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/pytorch/vision/blob/8c9585540a3e71f73ebc281445e888aa841824bd/torchvision/models/inception.py#L185" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/vision/blob/8c9585540a3e71f73ebc281445e888aa841824bd/torchvision/models/inception.py#L185" target="_blank" rel="nofollow noopener">pytorch/vision/blob/8c9585540a3e71f73ebc281445e888aa841824bd/torchvision/models/inception.py#L185</a></h4>
<pre class="onebox"><code class="lang-py"><ol class="start lines" start="175" style="counter-reset: li-counter 174 ;">
<li>    x = F.adaptive_avg_pool2d(x, (1, 1))</li>
<li>    # N x 2048 x 1 x 1</li>
<li>    x = F.dropout(x, training=self.training)</li>
<li>    # N x 2048 x 1 x 1</li>
<li>    x = torch.flatten(x, 1)</li>
<li>    # N x 2048</li>
<li>    x = self.fc(x)</li>
<li>    # N x 1000 (num_classes)</li>
<li>    return x, aux</li>
<li>
</li>
<li class="selected">@torch.jit.unused</li>
<li>def eager_outputs(self, x, aux):</li>
<li>    # type: (Tensor, Optional[Tensor]) -&gt; InceptionOutputs</li>
<li>    if self.training and self.aux_logits:</li>
<li>        return InceptionOutputs(x, aux)</li>
<li>    else:</li>
<li>        return x</li>
<li>
</li>
<li>def forward(self, x):</li>
<li>    x = self._transform_input(x)</li>
<li>    x, aux = self._forward(x)</li>
</ol></code></pre>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


ptrblck: <p>These failures all point to a torch version, which is too old, as both methods are relatively new.<br>
I just reinstalled PyTorch <code>1.5.0</code> with <code>torchvision</code> <code>0.6.0</code> and am able to import both libs as well as import <code>torch.jit.annotations.Optional</code>.</p>
<p>Could you check the path of torch via: <code>print(torch.__path__)</code> and make sure that it’s pointing to the right environment folder?</p>

rvandeghen: <p>It looks like the issue is there as my path does not include “anaconda”.</p>
<pre><code class="lang-auto">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__path__
['/home/renaud/.local/lib/python3.6/site-packages/torch']
</code></pre>
<p>even though the command <code>which python</code> includes the path of my conda env.</p>

ptrblck: <p>Could you run Python by specifying the complete conda path and try to import <code>torchvision</code> again?</p>

rvandeghen: <p>This is working when i’m in the folder in anaconda that contains torchvision.<br>
But i don’t know how to change the <code>sys.path</code> in python such that it import my packages from <code>anaconda/envs/...</code>.<br>
By the way I also don’t know why it has changed.<br>
Thank you for you help!<br>
Do you have a hint how I can change the path used for loading those packages ?</p>

rvandeghen: <p>Solved the problem of path with</p>
<pre><code class="lang-auto">export PYTHONNOUSERSITE=True
</code></pre>

ptrblck: <p>Thanks for posting the solution!<br>
I haven’t seen this path mismatch before in this manner, but I think it’ll be useful for others. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

dulanga_heshan: <aside class="quote no-group" data-username="rvandeghen" data-post="18" data-topic="80007">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/rvandeghen/48/23881_2.png" class="avatar"> rvandeghen:</div>
<blockquote>
<p>PYTHONNOUSERSITE=True</p>
</blockquote>
</aside>
<p>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</p>