Cannot import name 'PILLOW_VERSION' from 'PIL'

Aditya1: <p>when I execute -</p>
<pre><code class="lang-auto">from torchvision import datasets, transforms
</code></pre>
<p>I get the following error -</p>
<pre><code class="lang-auto">ImportError: cannot import name 'PILLOW_VERSION' from 'PIL' (C:\ProgramData\Anaconda3\lib\site-packages\PIL\__init__.py)
</code></pre>
<p>How can I fix this?</p>

Aditya1: <p>It appears that this is being fixed by PyTorch -<br>
<aside class="onebox githubissue">
  <header class="source">
      <a href="https://github.com/pytorch/vision/issues/1718" target="_blank" rel="nofollow noopener">github.com/pytorch/vision</a>
  </header>
  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/vision/issues/1718" target="_blank" rel="nofollow noopener">pillow 7.0.0 has removed `PILLOW_VERSION`</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2020-01-03" data-time="14:51:19" data-timezone="UTC">02:51PM - 03 Jan 20 UTC</span>
      </div>

        <div class="date">
          closed <span class="discourse-local-date" data-format="ll" data-date="2020-01-03" data-time="15:54:14" data-timezone="UTC">03:54PM - 03 Jan 20 UTC</span>
        </div>

      <div class="user">
        <a href="https://github.com/Gijs-Koot" target="_blank" rel="nofollow noopener">
          <img alt="Gijs-Koot" src="https://avatars2.githubusercontent.com/u/4848325?v=4" class="onebox-avatar-inline" width="20" height="20">
          Gijs-Koot
        </a>
      </div>
    </div>
  </div>
</div>

<div class="github-row">
  <p class="github-content">In the newest version of pillow, the attribute PILLOW_VERSION is removed in favor of PIL.__version__. torchvision relies on the old behaviour...</p>
</div>

<div class="labels">
    <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">bug</span>
    <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">duplicate</span>
    <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">module: transforms</span>
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>
<p>I found a work around by reverting PIL to 6.2.1</p>

abose: <p>As Aditya1 noted,<br>
conda install pillow=6.2.1<br>
will get around this for now.</p>

Brando_Miranda: <p>what is the minimum version of pytorch and torchvision for this error to stop?</p>

Brando_Miranda: <p>I was having an odd error:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "download_miniImagent.py", line 53, in &lt;module&gt;
    download_and_extract_miniImagenet(root=root)
  File "download_miniImagent.py", line 35, in download_and_extract_miniImagenet
    from torchvision.datasets.utils import download_file_from_google_drive, extract_archive
  File "/Users/brandBrandoParetoopareto/opt/miniconda3/lib/python3.7/site-packages/torchvision/__init__.py", line 2, in &lt;module&gt;
    from torchvision import datasets
  File "/Users/brandBrandoParetoopareto/opt/miniconda3/lib/python3.7/site-packages/torchvision/datasets/__init__.py", line 9, in &lt;module&gt;
    from .fakedata import FakeData
  File "/Users/brandBrandoParetoopareto/opt/miniconda3/lib/python3.7/site-packages/torchvision/datasets/fakedata.py", line 3, in &lt;module&gt;
    from .. import transforms
  File "/Users/brandBrandoParetoopareto/opt/miniconda3/lib/python3.7/site-packages/torchvision/transforms/__init__.py", line 1, in &lt;module&gt;
    from .transforms import *
  File "/Users/brandBrandoParetoopareto/opt/miniconda3/lib/python3.7/site-packages/torchvision/transforms/transforms.py", line 16, in &lt;module&gt;
    from . import functional as F
  File "/Users/brandBrandoParetoopareto/opt/miniconda3/lib/python3.7/site-packages/torchvision/transforms/functional.py", line 5, in &lt;module&gt;
    from PIL import Image, ImageOps, ImageEnhance, PILLOW_VERSION
ImportError: cannot import name 'PILLOW_VERSION' from 'PIL' (/Users/brandBrandoParetoopareto/opt/miniconda3/lib/python3.7/site-packages/PIL/__init__.py)
</code></pre>
<p>I installed pytorch and torchvision:</p>
<pre><code class="lang-auto">$ conda list
# packages in environment at /Users/brandBrandoParetoopareto/opt/miniconda3:
#
# Name                    Version                   Build  Channel
_pytorch_select           0.1                       cpu_0  
asn1crypto                1.3.0                    py37_0  
beautifulsoup4            4.8.2                    py37_0  
blas                      1.0                         mkl  
bzip2                     1.0.8                h1de35cc_0  
ca-certificates           2020.1.1                      0  
certifi                   2019.11.28               py37_1  
cffi                      1.14.0           py37hb5b8e2f_0  
chardet                   3.0.4                 py37_1003  
conda                     4.8.3                    py37_0  
conda-build               3.18.11                  py37_0  
conda-package-handling    1.6.0            py37h1de35cc_0  
cryptography              2.8              py37ha12b0ac_0  
filelock                  3.0.12                     py_0  
freetype                  2.9.1                hb4e5f40_0  
glob2                     0.7                        py_0  
idna                      2.8                      py37_0  
intel-openmp              2019.4                      233  
jinja2                    2.11.1                     py_0  
jpeg                      9b                   he5867d9_2  
libarchive                3.3.3                h786848e_5  
libcxx                    4.0.1                hcfea43d_1  
libcxxabi                 4.0.1                hcfea43d_1  
libedit                   3.1.20181209         hb402a30_0  
libffi                    3.2.1                h475c297_4  
libgfortran               3.0.1                h93005f0_2  
libiconv                  1.15                 hdd342a3_7  
liblief                   0.9.0                h2a1bed3_2  
libpng                    1.6.37               ha441bb4_0  
libtiff                   4.1.0                hcb84e12_0  
libxml2                   2.9.9                hf6e021a_1  
lz4-c                     1.8.1.2              h1de35cc_0  
lzo                       2.10                 h362108e_2  
markupsafe                1.1.1            py37h1de35cc_0  
mkl                       2019.4                      233  
mkl-service               2.3.0            py37hfbe908c_0  
mkl_fft                   1.0.15           py37h5e564d8_0  
mkl_random                1.1.0            py37ha771720_0  
ncurses                   6.2                  h0a44026_0  
ninja                     1.9.0            py37h04f5b5a_0  
numpy                     1.18.1           py37h7241aed_0  
numpy-base                1.18.1           py37h6575580_1  
olefile                   0.46                     py37_0  
openssl                   1.1.1e               h1de35cc_0  
pillow                    7.0.0            py37h4655f20_0  
pip                       20.0.2                   py37_1  
pkginfo                   1.5.0.1                  py37_0  
psutil                    5.7.0            py37h1de35cc_0  
py-lief                   0.9.0            py37h1413db1_2  
pycosat                   0.6.3            py37h1de35cc_0  
pycparser                 2.19                     py37_0  
pyopenssl                 19.1.0                   py37_0  
pysocks                   1.7.1                    py37_0  
python                    3.7.6                h359304d_2  
python-libarchive-c       2.8                     py37_13  
python.app                2                       py37_10  
pytorch                   1.4.0           cpu_py37hf9bb1df_0  
pytz                      2019.3                     py_0  
pyyaml                    5.3.1            py37h1de35cc_0  
readline                  7.0                  h1de35cc_5  
requests                  2.22.0                   py37_1  
ripgrep                   11.0.2               he32d670_0  
ruamel_yaml               0.15.87          py37h1de35cc_0  
setuptools                45.2.0                   py37_0  
six                       1.14.0                   py37_0  
soupsieve                 2.0                        py_0  
sqlite                    3.31.1               ha441bb4_0  
tk                        8.6.8                ha441bb4_0  
torchvision               0.2.1                      py_2    soumith
tqdm                      4.42.1                     py_0  
urllib3                   1.25.8                   py37_0  
wheel                     0.34.2                   py37_0  
xz                        5.2.4                h1de35cc_4  
yaml                      0.1.7                hc338f04_2  
zlib                      1.2.11               h1de35cc_3  
zstd                      1.3.7                h5bba6e5_0  
</code></pre>
<p>and I’ve had this script work in the past before I just don’t know what might be wrong…</p>
<p>saw this too… <a href="https://github.com/pytorch/vision/issues/1712" rel="nofollow noopener">https://github.com/pytorch/vision/issues/1712</a></p>

Brando_Miranda: <aside class="quote no-group" data-username="abose" data-post="3" data-topic="66096">
<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/abose/48/20278_2.png" class="avatar"> abose:</div>
<blockquote>
<p>conda install pillow=6.2.1</p>
</blockquote>
</aside>
<p>downgrade to</p>
<pre><code class="lang-auto">conda install pillow=6.2.1
</code></pre>
<p>to temporarily solve this issue.</p>

blackberry: <p>Hello…</p>
<p>I have just installed PyTorch 1.12 on a EC2 DL AMI (originally PyTorch 1.1 DL AMI) using conda using the command on PyTorch page as below, and now see this error message upon importing torchvision. After installation, PIL version is 9.1.1.</p>
<pre><code class="lang-auto">conda create -n pytorch12 python=3.10.5 ipython
conda install pytorch torchvision torchaudio cudatoolkit=11.6 -c pytorch -c conda-forge
</code></pre>
<p>The original conda env with PyTorch 1.11 works fine. Interestingly, the AMI and PyTorch 1.1 env have PIL version 9.2.0. Installing PyTorch 1.12 with the above command seems to downgrade PIL to 9.1.1.</p>
<p>Is there a clean solution? How should I install PyTorch 1.12 in a conda env?</p>
<p><strong>Update:</strong> Solved<br>
I used pip to install pytorch and also specified PIL (pillow) version as 9.2.0 and the issue was solved.</p>
<pre><code class="lang-auto">conda create -n pytorch12 python=3.10.5 ipython
pip3 install torch torchvision torchaudio 'pillow==9.2.0' --extra-index-url https://download.pytorch.org/whl/cu116
</code></pre>
<p>Thanks!</p>