Python 3,PIL是Pillow的别名吗?

6

在Windows 7 64位上安装Python 3.5。

我很困惑——如果安装了Pillow,那么是否必须使用已废弃/未安装的图形库PIL作为其别名?

根据Pillow的安装说明(http://pillow.readthedocs.io/en/3.3.x/installation.html),"Pillow和PIL不能在同一环境中共存。在安装Pillow之前,请卸载PIL。" 好的:

D:\Python35>pip uninstall PIL

Cannot uninstall requirement PIL, not installed

接下来:

D:\Python35>pip install Pillow

Requirement already satisfied (use --upgrade to upgrade): Pillow in d:\python35\lib\site-package

所以现在:

D:\Python35>python

Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:27:37) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.

import Pillow

Traceback (most recent call last):

File "stdin", line 1, in module

ImportError: No module named 'Pillow'

好的,所以如果没有Pillow(或pillow),让我们尝试:

import PIL

即使pip告诉我未安装PIL,也没有错误。接下来:

dir(PIL)

['PILLOW_VERSION', 'VERSION', 'builtins', 'cached', 'doc', 'file', 'loader'

嗯,似乎是矛盾的命名,比如PIL与Pillow?

PIL.PILLOW_VERSION

'3.4.1'

截至本文日期,3.4.1是Pillow的最新版本(https://pypi.python.org/pypi/Pillow

我非常困惑——需要使用PIL作为Pillow的别名似乎是错误的。但根据上述情况,似乎必须这样做?

1个回答

7

PillowPIL 库的继任者。它仍然使用 pil 包名称作为一种兼容性替代。

  • 安装 Pillow
  • import pil

pip 包名称和 Python 的 import 包名称不必相同。由于 Pillow 旨在取代 PIL,因此使用相同的 import 名称以实现兼容性。


我愿意接受这一点,但它似乎非常违反直觉:Pillow文档告诉我Pillow不能与PIL共存,但随后又重复使用了它的名称。因此,我的后续问题是,是否有一种简单的方法可以“导入Pillow”以避免对使用哪个库存在歧义? - RBV
5
因为使用相同的名称,所以PIL和RBV无法共存。在使用import命令导入模块时,只能安装一个名为PIL的模块,它要么是Pillow,要么是PIL。 - MisterMiyagi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接