# 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' (/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.
# ⛔️ old import (Pillow<7.0) from PIL import PILLOW_VERSION
With the following import statement.
# ✅ new import (Pillow>7.0) from PIL import __version__ print(__version__)
一个替代的 hacky 解决方案是在入口点文件的顶部添加以下行。
import PIL from PIL import __version__ PIL.PILLOW_VERSION = __version__ print(PIL.PILLOW_VERSION)
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__)
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)
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.
# 👇️ 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.
pip freeze > requirements.txt