ImportError: Cannot import name ‘PILLOW_VERSION’ from ‘PIL’

# ImportError: Cannot import name ‘PILLOW_VERSION’ from ‘PIL’

ImportError: cannot import name ‘PILLOW_VERSION’ from ‘PIL’

The “ImportError: cannot import name ‘PILLOW_VERSION’ from ‘PIL'” occurs
because starting Pillow version 7.0.0, PILLOW_VERSION has been removed and
renamed to __version__.

To solve the error, correct your import statements or downgrade Pillow to the
last version before 7.0.0.

importerror cannot import name pillow version from pil

shell
ImportError: cannot import name 'PILLOW_VERSION' from 'PIL' (/home/borislav/Desktop/bobbyhadz_python/venv/lib/python3.11/site-packages/PIL/__init__.py)

# Use the correct import statement

If the error occurred in your code, make sure to replace the following import
statement.

main.py
# ⛔️ old import (Pillow<7.0) from PIL import PILLOW_VERSION

With the following import statement.

main.py
# ✅ new import (Pillow>7.0) from PIL import __version__ print(__version__)

import version from pil

一个替代的 hacky 解决方案是在入口点文件的顶部添加以下行。

主程序
import PIL from PIL import __version__ PIL.PILLOW_VERSION = __version__ print(PIL.PILLOW_VERSION)

patch pil import statement

代码示例只是将PILLOW_VERSION属性设置为 的值__version__

使用 try/except 语句来处理两个 Pillow 版本

try/except如果需要处理两个 Pillow 版本,也可以使用语句。

主程序
try: # 👇️ Pillow > 7.0 from PIL import __version__ except ImportError: # 👇️ Pillow < 7.0 from PIL import PILLOW_VERSION as __version__ print(__version__)

import using try except statement

try如果您的 Pillow 版本大于 ,则语句运行7.0否则,except块运行,我们使用旧import语句。

降级

或者,您可以将Pillow降级到 之前的最后一个版本7.0.0,这是包含导出的最后一个版本
PILLOW_VERSION

如果您环境中的其他包正在使用
PILLOW_VERSION并且您无法轻松更新它们,这将很有用。

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"

当安装了 Pillow 6.XY 版本时,您可以使用以下导入语句。

主程序
from PIL import PILLOW_VERSION

运行该命令时,您可能会收到一条错误消息,指出“错误:pip 的依赖项解析器当前未考虑所有已安装的包。” .

但是,即使显示错误,软件包也已成功安装。

如果这些建议都没有帮助,您可以尝试升级您环境中的所有包。

升级你环境中的所有包

升级所有过时包的最直接方法是使用 Python 脚本。

主程序
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)
You can store the script in a Python file, e.g. main.py and run the file with python main.py to upgrade all of the outdated packages.

Here are alternative commands you can use to upgrade all outdated packages.

shell
# 👇️ macOS or Linux pip install -U `pip list --outdated | awk 'NR>2 {print $1}'` # 👇️ Windows for /F "delims= " %i in ('pip list --outdated') do pip install -U %i

If you use a requirements.txt file, you can update it with the following
command.

shell
pip freeze > requirements.txt