Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError: cannot import name 'PILLOW_VERSION' from 'PIL' (unknown location) #4130

Closed
poormag opened this issue Oct 11, 2019 · 47 comments
Closed
Labels
Anaconda Issues with Anaconda's Pillow Installation Usually a problem with … Linux

Comments

@poormag
Copy link

poormag commented Oct 11, 2019

I got this error while importing Image

from PIL import Image
Traceback (most recent call last):

  File "<ipython-input-13-b7f01c2f8cfe>", line 1, in <module>
    from PIL import Image

  File "/lib/python3.7/site-packages/PIL/Image.py", line 40, in <module>
    from . import PILLOW_VERSION, ImageMode, TiffTags, __version__, _plugins

**ImportError:** cannot import name 'PILLOW_VERSION' from 'PIL' (unknown location)

I tried to remove it by myself but then got the same problem with __version__, then _plugins

Updating doesn't work for me. Can you help please?

@radarhere
Copy link
Member

How did you install Pillow?

Googling, I would conclude that there is something strange about your installation, rather than anything specific to Pillow. I would be curious about what is printed by this code, and whether it makes a difference -

import sys
print(sys.path)
sys.path.append('/lib/python3.7/site-packages')
from PIL import Image

@poormag
Copy link
Author

poormag commented Oct 11, 2019

@radarhere i did it by usual way pip3 install Pillow.

After this:

import sys
print(sys.path)
sys.path.append('/lib/python3.7/site-packages')
from PIL import Image

I got another an import error but different:
ImportError: cannot import name '_imaging' from 'PIL' (/lib/python3.7/site-packages/PIL/__init__.py).

Should I reinstall it by some specific way?

@radarhere
Copy link
Member

Installing it through pip should be fine. What operating system are you using?

Have you tried uninstalling it and re-installing it to see if that fixes anything?

@poormag
Copy link
Author

poormag commented Oct 11, 2019

@radarhere , yeah, of course I did...
Ubuntu 18.0

Thanx for you help, now I see it's maybe some environment problem.

@hugovk hugovk added the Installation Usually a problem with … label Oct 11, 2019
@radarhere
Copy link
Member

Another thought - considering that the first problem means that Python wasn't able to import the package correctly in some way, you might try python -m pip install Pillow instead of just pip install Pillow, as that will ensure that the same Python is being used for pip as for the running of your script.

@radarhere
Copy link
Member

@poormag if you resolve this, be sure to let us know, so we can close the issue, and maybe leave a note of the solution for the sake of anyone arriving here in the future.

Thanks for using Pillow. Unfortunately it is harder to remotely debug install problems.

@radarhere
Copy link
Member

For _imaging, I would check in the installed PIL directory to see if there is an _imaging*.so file.

For another idea, import PIL.Image as Image might conceivably produce a different result.

Python 3.8 has also been released. While not a solution to your problem, you may be interested in upgrading, and that new environment may work for you.

@stmax82
Copy link

stmax82 commented Nov 19, 2019

I'm getting the same error right now after conda install pillow on Win 10.
It installed pillow 6.2.1, python is 3.6.9.

Looks like downgrading to pillow 6.1 helps.. after conda install pillow=6.1 the error is gone.

@hugovk
Copy link
Member

hugovk commented Nov 19, 2019

@stmax82 Please report Conda issues to https://github.com/conda-forge/pillow-feedstock.

It sounds a bit like conda-forge/pillow-feedstock#66 ("DLL import fails in fresh conda install"), fixed yesterday.


I'll close this issue, @poormag let us know if this is still a problem with pip and we can re-open.

@hugovk hugovk closed this as completed Nov 19, 2019
@WeiQijie
Copy link

WeiQijie commented Jan 3, 2020

I met the same problem. I am using python3.7.5 on macos. When using pip install Pillow, pillow 7.0.0 was installed. I received ImportError: cannot import name 'PILLOW_VERSION' from 'PIL'. When I specify the version pip install Pillow==6.1, the problem is gone.

@hugovk
Copy link
Member

hugovk commented Jan 3, 2020

@WeiQijie In 7.0.0:

PILLOW_VERSION has been removed. Use __version__ instead.

https://pillow.readthedocs.io/en/stable/releasenotes/7.0.0.html#pillow-version-constant

@Gauraviiitian
Copy link

Gauraviiitian commented Jan 3, 2020

I met the same problem. I am using python3.7.5 on macos. When using pip install Pillow, pillow 7.0.0 was installed. I received ImportError: cannot import name 'PILLOW_VERSION' from 'PIL'. When I specify the version pip install Pillow==6.1, the problem is gone.

This one worked for me, thanks for sharing, I really struggled with this the whole Day. Thanks again!

@hugovk
Copy link
Member

hugovk commented Jan 3, 2020

I recommend updating your code to use from PIL import __version__ instead of from PIL import PILLOW_VERSION (or similar).

__version__ has been available since Pillow 3.4.0 (October 2016, #2027), so works in Pillow 6. You should require pillow>=3.4.0, and I suggest using the latest 7.0.0 as several security fixes have been made since 3.4.0.

If you still need to work with pillow<3.4.0:

try:
    from PIL import __version__
except ImportError:
    from PIL import PILLOW_VERSION as __version__

@clint-bg
Copy link

clint-bg commented Jan 3, 2020

I had the same error when using docker to build a Linux based image in Windows 10 with fastai. Added "RUN pip install Pillow==6.1" above "RUN pip install fastai" in the Dockerfile and following the build, the container successfully ran. Thanks for your posts.

@addypy
Copy link

addypy commented Jan 4, 2020

had the same error while using pytorch code which had deprecated pillow code. since PILLOW_VERSION was deprecated, i worked around it by:

Simply duplicating the _version file and renaming it as PILLOW_VERSION.py in the same folder.

worked for me

@jamessmith90
Copy link

jamessmith90 commented Jan 14, 2020

@WeiQijie In 7.0.0:

PILLOW_VERSION has been removed. Use __version__ instead.

https://pillow.readthedocs.io/en/stable/releasenotes/7.0.0.html#pillow-version-constant

perhaps one of the most stupid things ever done. Boycotting 7.0.0 version Rolling back to 6.2.2

@hugovk
Copy link
Member

hugovk commented Jan 16, 2020

torchvision v0.5.0 has been released with the fix:

  1. Require torchvision>=0.5.0
  2. If Pillow was temporarily pinned, remove the pin

@asears
Copy link

asears commented Jan 19, 2020

@hugovk can someone release the Windows version of torchvision 0.5.0 to pipy and conda?
https://github.com/pytorch/vision/tree/master/packaging

Last version with a Windows wheel is https://pypi.org/project/torchvision/0.4.1/#files
Looks like something dropped the Windows versions after that?
https://pypi.org/project/torchvision/0.5.0/#files

@hugovk
Copy link
Member

hugovk commented Jan 19, 2020

@asears Please ask at the torchvision project. This might be the right place: pytorch/vision#1717

(Edit: fixed link, but pytorch/vision#1756 seems relevant and I see you already commented there 👍)

@mirror0013
Copy link

mirror0013 commented Apr 2, 2020 via email

@shanethomas1029
Copy link

shanethomas1029 commented Apr 5, 2020

Try this method:
First, uninstall the Pillow package from your system

  1. python -m pip uninstall Pillow
    Second, Reinstall it!
  2. python -m pip install Pillow

Simple method!

if the Error is (Given below), the above method will work.

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    from PIL import Image
  File "C:\Users\shane\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 44, in <module>
    from . import (
ImportError: cannot import name '_raise_version_warning' from 'PIL' (C:\Users\shane\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\__init__.py)

The method is tested on windows and ubuntu

@radarhere
Copy link
Member

@shanethomas1029 hi, sorry, not quite following. What triggered the error cannot import name 'raise_version_warning' from 'PIL'?

@mirror0013
Copy link

mirror0013 commented Apr 5, 2020 via email

@shanethomas1029
Copy link

@shanethomas1029 hi, sorry, not quite following. What triggered the error cannot import name 'raise_version_warning' from 'PIL'?

You got it?

@shanethomas1029
Copy link

I's already done: PILLOW_VERSION='7.0.0' And it's working ... вс, 5 апр. 2020 г. в 14:17, shanethomas1029 notifications@github.com:
Try this method: First, uninstall the Pillow package from your system 1. python -m pip uninstall Pillow Second, Reinstall it! 2. python -m pip install Pillow Simple method! if the Error is (Given below), the above method will work. Traceback (most recent call last): File "main.py", line 3, in from PIL import Image File "C:\Users\shane\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 44, in from . import ( ImportError: cannot import name 'raise_version_warning' from 'PIL' (C:\Users\shane\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL_init .py) The method is tested on windows and ubuntu — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#4130 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALH6ZJGEDHWKZ2GMGYGJIB3RLBSERANCNFSM4I7XCONQ .
from torchvision import models from torchvision import datasets from torchvision import ops from torchvision import transforms from torchvision import utils from torchvision import io try: from .version import version # noqa: F401 except ImportError: pass _image_backend = 'PIL' PILLOW_VERSION='7.0.0' def set_image_backend(backend): """ Specifies the package used to load images. Args: backend (string): Name of the image backend. one of {'PIL', 'accimage'}. The :mod:accimage package uses the Intel IPP library. It is generally faster than PIL, but does not support as many operations. """ global _image_backend if backend not in ['PIL', 'accimage']: raise ValueError("Invalid backend '{}'. Options are 'PIL' and 'accimage'" .format(backend)) _image_backend = backend def get_image_backend(): """ Gets the name of the package used to load images """ return _image_backend

Okay
Thanks

@mirror0013
Copy link

mirror0013 commented Apr 6, 2020 via email

@ugurkanates
Copy link

Such an unnecessary changes. It almost feel like people here are bored and trying to find ways to broke old codes.

Thanks for fix btw @zealousfool

@hugovk
Copy link
Member

hugovk commented Apr 9, 2020

PILLOW_VERSION was re-added in Pillow 7.1.0, released 2020-04-01, to give projects more time to upgrade:

@Greg-Tarr
Copy link

PILLOW_VERSION was re-added in Pillow 7.1.0, released 2020-04-01, to give projects more time to upgrade:

I can verify that:

python -m pip uninstall Pillow
python -m pip install Pillow

Reinstalling/installing Pillow 7.1.0 fixes the problem.

@mirror0013
Copy link

mirror0013 commented Apr 10, 2020 via email

@lxgyChen
Copy link

lxgyChen commented May 9, 2020

Add the following line to your_path\Anaconda\Lib\site-packages\PIL_init_.py:

PILLOW_VERSION = _version.__version__

@Jirigesi
Copy link

I met the same problem. I am using python3.7.5 on macos. When using pip install Pillow, pillow 7.0.0 was installed. I received ImportError: cannot import name 'PILLOW_VERSION' from 'PIL'. When I specify the version pip install Pillow==6.1, the problem is gone.

This one worked for me, thanks for sharing, I really struggled with this the whole Day. Thanks again!

This works for me as well!

fboylu added a commit to microsoft/computervision-recipes that referenced this issue Jun 16, 2020
* Syncing staging <> master branches (#476)

* Removed submodules.

* Add back submodules using https://

* simplified plotting functions

* fixed most tests

* fixed test

* fixed unit test

* small text edits to the 02 notebook

* added fct description

* tiny cleanup on notebook

* Add pretrained keypoint model (#453)

* Add pretrained keypoint model

* Fix bugs in tests

* Add 03 notebook in conftest.py

* Minor revision

* Reformat code using black

* if folder exists, remove (#448)

* Add mask annotation tool (#447)

* Add mask annotation tool

* Update mask annotation explanation and add converion scripts

* Add screenshots of Labelbox annotation

* Rearrange screenshots

* Move convertion script into functions in data.py

* Point out annotation conversion scripts clearly in notebook

* Refine annotation conversion scripts

* Fix bugs

* Add tests for labelbox format conversion methods

* Move r2p1d from contrib to scenarios.

* Update .gitignore.

* Add README.md

* Remove the folder /scenario/action_recognition/data/samples; update notebook to use web url for sample data.

* Move data split files to data/misc; update notebook accordingly.

* Update data path.

* Add keypoint detection with tuned model (#454)

* Add keypoint detetion with tuned model

* Add tests

* Minor revision

* Update tests

* Fix bugs in tests

* Use GPU device if available

* Update tests

* Fix bug: 'not idx' will be 'True' if 'idx=0'

* Fix bugs

* Move toy keypoint meta into notebook

* Fix bugs

* Fix bugs

* Fix bugs in notebook

* Add descriptions for keypoint meta data

* Raise exception when RandomHorizontalFlip is used without specifying hflip_inds

* Add NOTICE file.

* Add keypoint detection model tuning with top and bottom keypoints (#456)

* Add keypoint detection model tuning with top and bottom keypoints

* Fix undefined unzip_url

* Resolved undefined od_urls

* Add annotation tool to scenarios.

* Plot keypoints as round dots to make them noticeable (#458)

* Plot keypoints as dots

* Change variable naming

* Resolve test machine failure (#460)

This is due to the latest PyTorch (version 1.3) from conda is built on
CUDA 10.1 while the version on the test machine is CUDA 10.0.

* Remove unused imports in 02_mask_rcnn.ipynb (#463)

* Remove unused imports in 02_mask_rcnn.ipynb

* Add missing imports

* Simplify binary_mask() (#464)

* clean up of keypoint detection notebook

* minor clean-up

* remove conflict code (#471)

* Update README.md (#472)

* updated readmes

* added images

* updated readmes

* added intro figure

* modified new picture

* update figure

* Move to bare Windows GPU VMs and fix build issues (#475)

* Updating AzureDevOps pipeline agent pool to use a barebones Windows GPU VM.

* Added a AzureDevOps pipeline for windows.

* Added a conda init step for powershell.

* Adding conda to PATH as a separate step

* Pinning pillow to 6.1 to fix issues listed in: python-pillow/Pillow#4130

* chained powershell commands

* Updated AzureDevOps yml file to use the inline powershell script syntax.

* Moved to using inline commands + Invoke-Expression syntax

* Adding indentation for chained commands.

* Moving to the literal block style indicator for yaml for chained commands.

* Using call activate instead of source activate when activating the conda environment through powershell.

* Switching to script steps instead of using powershell steps for conda operations.

* Fixed the numpy.float64 issue by moving to a fork with the upstream fixes for the issue.

* Fixed the numpy.float64 issue in utils/cv/detection/plot.py

Co-authored-by: PatrickBue <pabuehle@microsoft.com>
Co-authored-by: Simon Zhao <43029286+simonzhaoms@users.noreply.github.com>
Co-authored-by: Miguel González-Fierro <3491412+miguelgfierro@users.noreply.github.com>
Co-authored-by: JS <jiata@microsoft.com>

* fix to load from saved mask-rcnn model

* fix to load from saved mask-rcnn model

* fix to load from saved mask-rcnn model

Co-authored-by: Young Park <youngpark@cs.stanford.edu>
Co-authored-by: PatrickBue <pabuehle@microsoft.com>
Co-authored-by: Simon Zhao <43029286+simonzhaoms@users.noreply.github.com>
Co-authored-by: Miguel González-Fierro <3491412+miguelgfierro@users.noreply.github.com>
Co-authored-by: JS <jiata@microsoft.com>
@DAVID-Hown
Copy link

DAVID-Hown commented Feb 3, 2021

Hello, I have a similar problem, could you please help me check the error:

Traceback (most recent call last):
  File "E:/ShadowCreative/Siamese network/pytorch_Siamese_network/siamese-pytorch/train.py", line 80, in <module>
    for batch_id, (img1, img2, label) in enumerate(trainLoader, 1):
  File "C:\Users\dawei.hao\Anaconda3\lib\site-packages\torch\utils\data\dataloader.py", line 346, in __next__
    data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
  File "C:\Users\dawei.hao\Anaconda3\lib\site-packages\torch\utils\data\_utils\fetch.py", line 44, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "C:\Users\dawei.hao\Anaconda3\lib\site-packages\torch\utils\data\_utils\fetch.py", line 44, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "E:\ShadowCreative\Siamese network\pytorch_Siamese_network\siamese-pytorch\mydataset.py", line 62, in __getitem__
    image1 = self.transform(image1)
  File "C:\Users\dawei.hao\Anaconda3\lib\site-packages\torchvision\transforms\transforms.py", line 61, in __call__
    img = t(img)
  File "C:\Users\dawei.hao\Anaconda3\lib\site-packages\torchvision\transforms\transforms.py", line 1117, in __call__
    return F.affine(img, *ret, resample=self.resample, fillcolor=self.fillcolor)
  File "C:\Users\dawei.hao\Anaconda3\lib\site-packages\torchvision\transforms\functional.py", line 789, in affine
    kwargs = {"fillcolor": fillcolor} if PILLOW_VERSION[0] >= '5' else {}
NameError: name 'PILLOW_VERSION' is not defined

@radarhere
Copy link
Member

@DAVID-Hown I recommend that you upgrade Pillow to at least 7.1.0, or torchvision to at least 0.5.0.

@radarhere
Copy link
Member

As has been previously stated, PILLOW_VERSION was brought back in Pillow 7.1.0.

In case it is a concern to anyone in this thread, Pillow 9.0.0, due out on January 1, will remove PILLOW_VERSION again.

@ashutosh1919
Copy link

pip install "Pillow<7.0" --no-deps worked for me for Python 3.8

@raktim-mondol
Copy link

raktim-mondol commented Jun 25, 2022

Another thought - considering that the first problem means that Python wasn't able to import the package correctly in some way, you might try python -m pip install Pillow instead of just pip install Pillow, as that will ensure that the same Python is being used for pip as for the running of your script.

The following code fixed the problem (python 3.7, linux):
python -m pip install pillow==6.2.2 --user

@lamoboos223
Copy link

This is what I did and worked out perfectly

cd /home/ubuntu/.local/lib/python3.7/site-packages/PIL

cp _version.py PILLOW_VERSION.py

@nguyenvulong
Copy link

Try this in the script that tries to import PILLOW_VERSION

from PIL import __version__ as PILLOW_VERSION

not guaranteed to work with all cases though, you need to dig up a bit

This is so much headache

@youngday

This comment was marked as off-topic.

@runjia0124
Copy link

@WeiQijie In 7.0.0:

PILLOW_VERSION has been removed. Use __version__ instead.

https://pillow.readthedocs.io/en/stable/releasenotes/7.0.0.html#pillow-version-constant

This is definitely what I am looking for, especially in cases pillow or python version can not be downgraded!

@radarhere
Copy link
Member

Hi. If anyone has a problem with this, please open a new issue to discuss it. We would like people to use the latest version of Pillow possible, so downgrading is not an ideal solution. If the problem is another library still using PILLOW_VERSION, please request that the maintainers of that library update their code.

@python-pillow python-pillow locked as resolved and limited conversation to collaborators Sep 24, 2022
@aclark4life aclark4life added the Anaconda Issues with Anaconda's Pillow label May 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Anaconda Issues with Anaconda's Pillow Installation Usually a problem with … Linux
Projects
None yet
Development

No branches or pull requests