python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
12.24k stars 2.23k forks source link

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

Closed poormag closed 4 years ago

poormag commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

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

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

radarhere commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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 commented 4 years ago

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 commented 4 years ago

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

It sounds a bit like https://github.com/conda-forge/pillow-feedstock/issues/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.

WeiQijie commented 4 years ago

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 commented 4 years ago

@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 commented 4 years ago

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 commented 4 years ago

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, https://github.com/python-pillow/Pillow/pull/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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

@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 commented 4 years ago

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 commented 4 years ago

@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 commented 4 years ago

@asears Please ask at the torchvision project. This might be the right place: https://github.com/pytorch/vision/pull/1717

(Edit: fixed link, but https://github.com/pytorch/vision/issues/1756 seems relevant and I see you already commented there 👍)

kumarnikhil936 commented 4 years ago

Problem is the fact that PILLOW_VERSION is removed in 7.0 Either one can downgrade to the previous build (works for me) or better is that this change gets reflected in the touchvision source code.

franva commented 4 years ago

in my case, I have Python 3.7.6 and I downgraded to the latest possible match

conda install pillow=6.2.1
zealousfool commented 4 years ago

I fixed the problem by just adding the code ‘PILLOW_VERSION='7.0.0'’ to the file '.../PIL/init.py'. I don't know if there will be any problems, but it works now.

mirror0013 commented 4 years ago

I fixed the problem by just adding the code ‘PILLOW_VERSION='7.0.0'’ to the file '.../PIL/init.py'. I don't know if there will be any problems, but it works now.

It's working! It's really working! (Just adding the code PILLOW_VERSION='7.0.0' to the beginning of the file '.../Lib/site-packages/PIL/init.py'.)

Antupis commented 4 years ago

I just set pillow version when I imported code

import PIL

PIL.PILLOW_VERSION='7.0.0'

import PIL.Image as Image

mirror0013 commented 4 years ago

IDK

чт, 2 апр. 2020 г. в 11:33, Antupis notifications@github.com:

I just set pillow version when I imported code ` import PIL

PIL.PILLOW_VERSION='7.0.0'

import PIL.Image as Image `

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/python-pillow/Pillow/issues/4130#issuecomment-607702538, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALH6ZJDDNPZS6N24MBH5PB3RKREWPANCNFSM4I7XCONQ .

shanethomas1029 commented 4 years ago

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 commented 4 years ago

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

mirror0013 commented 4 years ago

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 https://github.com/python-pillow/Pillow/issues/4130#issuecomment-609400203, 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

shanethomas1029 commented 4 years ago

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

You got it?

shanethomas1029 commented 4 years ago

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 commented 4 years ago

OK

пн, 6 апр. 2020 г. в 12:37, shanethomas1029 notifications@github.com:

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

You got it?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/python-pillow/Pillow/issues/4130#issuecomment-609685526, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALH6ZJGTEXBWAQLAVYKTROLRLGPEPANCNFSM4I7XCONQ .

ugurkanates commented 4 years ago

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 commented 4 years ago

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

Greg-Tarr commented 4 years ago

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 commented 4 years ago

Hello. OK. Thank you! It's working now too. I DID it! It's more prefer to use some installations than to write the code ... Good-bye!

пт, 10 апр. 2020 г. в 18:30, Greg Tarr notifications@github.com:

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

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

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

I can verify that:

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

Reinstalling/installing Pillow 7.1.0 fixes the problem.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/python-pillow/Pillow/issues/4130#issuecomment-612079762, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALH6ZJE7W6H5UQL4BC7QDNTRL43RXANCNFSM4I7XCONQ .

lxgyChen commented 4 years ago

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

PILLOW_VERSION = _version.__version__

Jirigesi commented 4 years ago

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!

DAVID-Hown commented 3 years ago

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 commented 3 years ago

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

radarhere commented 2 years ago

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 commented 2 years ago

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

raktim-mondol commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

@nguyenvulong thanks

runjia0124 commented 2 years ago

@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 commented 2 years ago

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.