Importerror: cannot import name ‘pillow_version’ from ‘pil’

If you’ve been using Python for a while, chances you have come across the error Importerror: cannot import name ‘pillow_version’ from ‘pil’.

Here is how this error occurs:

from PIL import Image
from PIL import pillow_version

image = Image.open("example.jpg")
print(f"Pillow version: {pillow_version}")
image.show()

Output:


ImportError: cannot import name 'pillow_version' from 'PIL' 

So in this article, we will explore the different causes of this error and provide you with effective solutions to fix it.

Importerror: cannot import name pillow_version from pil

We got Importerror: cannot import name pillow_version from pil error because pillow_version was removed and replaced by _version_ starting from pillow version 7.0.0.

The straightforward way we can do to fix this error is to downgrade the pillow version before 7.0.0.

cannot import name ‘pillow_version’ from ‘pil -Solutions

To fix this error consider the following solutions and try to troubleshoot the error.

Solution 1: Downgrade the Pillow module

One way to fix the error is to downgrade the pillow module before 7.0.0. Since it is the last version where PILLOW_VERSION is available.

In addition, this downgrade is useful when the packages in the environment can’t be easily updated and still using PILLOW_VERSION.

Here are commands you can use.

pip install "pillow<7"
pip3 install "pillow<7"

python -m pip install "pillow<7"
python3 -m pip install "pillow<7"
py -m pip install "pillow<7"

# 📌 for Anaconda
conda install -c conda-forge "pillow<7"

# 📌 for Jupyter notebook
!pip install "pillow<7"

Meanwhile, if you are using the 6.xx version of the pillow you can use the commands as follows:

from PIL import PILLOW_VERSION

Take note: Running this command may output “ERROR: pip’s dependency resolver does not currently take into account all the packages that are installed”. Don’t worry the package is still installed successfully.

Solution 2: Use different function

If you want to find the version of the installed pillow then use ” __version__ ” instead of pillow_version. It’s because the newest version of the pillow module doesn’t support this method.

Another way to fix the error is to use a different function. Actually, if you don’t know the version and you want to show it, use the ‘__version__‘ instead of pillow_version. Apparently, this method is not supported any more in the newest version.

from PIL import __version__
print(__version__)

Output

7.1.2

Solution 3: Update import statement

Alternatively, if the error happens upon coding try to update import statements.

From this:

# ❌ old import (Pillow<7.0)
from PIL import PILLOW_VERSION

To this:

# ✅ new import (Pillow>7.0)
from PIL import __version__

print(__version__)

Solution 4: Use try/except statement to handle both Pillow versions

Alternatively, you can also use a try/except statement to handle the different versions of Pillow.

Here is the command you can try:

try:
    #📌Pillow > 7.0
    from PIL import __version__
except ImportError:
    # 📌 Pillow < 7.0
    from PIL import PILLOW_VERSION as __version__

print(__version__)

The try statement runs if your Pillow version is greater than 7.0, otherwise, the except block runs and we use the older import statement.


Solution 5: Upgrade all package environment

Lastly, the direct way to upgrade all outdated packages is to use a Python script.

import pkg_resources
from subprocess import call

packages = [dist.project_name for dist in pkg_resources.working_set]
call("pip install --upgrade " + ' '.join(packages), shell=True)

Example Fixed Error Code

Here is now the correct way to import the version of Pillow that you are using by importing it from the “__version__” attribute of the “PIL” module:

from PIL import Image
from PIL import __version__ as pillow_version

image = Image.open("example.jpg")
print(f"Pillow version: {pillow_version}")
image.show()

Common mistakes to avoid Importerror

Common why reasons why encounter this “ImportError: cannot import name pillow_version from PIL” error:

  • The Pillow package is not installed or is not installed correctly on your system.
  • The version of Pillow that you have installed is incompatible with the version of PIL that you are using.
  • There may be a typo or other error in the code that is causing the ImportError.
  • There may be a conflict with other packages or modules that are installed on your system.
  • Your Python environment may be set up incorrectly, or you may be using the wrong version of Python.

Anyway here are some other fixed errors wherein you can refer to try when you might face these errors:

Conclusion

In conclusion, Importerror: cannot import name pillow_version from pil occurs when you import pillow_version because it was removed and replaced by __version__ starting from pillow version 7.0.0.

I hope this article has helped you fix the error.

Until next time! 😊