如何在Python 3.6上安装PIL/Pillow?

76

我有一个脚本需要用到PIL才能运行。除了降级Python外,我找不到其他安装PIL到Python 3.6的方法。

以下是我的尝试:

pip install pil
Collecting pil
  Could not find a version that satisfies the requirement pil (from versions: )
No matching distribution found for pil

pip install Pillow
Collecting Pillow
  Using cached Pillow-3.3.1.zip
Installing collected packages: Pillow
  Running setup.py install for Pillow ... error
    Complete output from command c:\python\python36\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\ABDULR~1\\AppData\\Local\\Temp\\pip-build-rez5zpri\\Pillow\\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record C:\Users\ABDULR~1\AppData\Local\Temp\pip-a5bugnjo-record\install-record.txt --single-version-externally-managed --compile:
    Single threaded build for windows
    running install
    running build
    running build_py
    creating build
    creating build\lib.win-amd64-3.6
    creating build\lib.win-amd64-3.6\PIL
    copying PIL\......................
    ..................................
    ..................................
    running egg_info
    writing Pillow.egg-info\PKG-INFO
    writing dependency_links to Pillow.egg-info\dependency_links.txt
    writing top-level names to Pillow.egg-info\top_level.txt
    warning: manifest_maker: standard file '-c' not found

    reading manifest file 'Pillow.egg-info\SOURCES.txt'
    reading manifest template 'MANIFEST.in'
    warning: no files found matching '*.sh'
    no previously-included directories found matching 'docs\_static'
    warning: no previously-included files found matching '.coveragerc'
    warning: no previously-included files found matching '.editorconfig'
    warning: no previously-included files found matching '.landscape.yaml'
    warning: no previously-included files found matching 'appveyor.yml'
    warning: no previously-included files found matching 'build_children.sh'
    warning: no previously-included files found matching 'tox.ini'
    warning: no previously-included files matching '.git*' found anywhere in distribution
    warning: no previously-included files matching '*.pyc' found anywhere in distribution
    warning: no previously-included files matching '*.so' found anywhere in distribution
    writing manifest file 'Pillow.egg-info\SOURCES.txt'
    copying PIL\OleFileIO-README.md -> build\lib.win-amd64-3.6\PIL
    running build_ext
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\ABDULR~1\AppData\Local\Temp\pip-build-rez5zpri\Pillow\setup.py", line 753, in <module>
        zip_safe=not debug_build(), )
      File "c:\python\python36\lib\distutils\core.py", line 148, in setup
        dist.run_commands()
      File "c:\python\python36\lib\distutils\dist.py", line 955, in run_commands
        self.run_command(cmd)
      File "c:\python\python36\lib\distutils\dist.py", line 974, in run_command
        cmd_obj.run()
      File "c:\python\python36\lib\site-packages\setuptools\command\install.py", line 61, in run
        return orig.install.run(self)
      File "c:\python\python36\lib\distutils\command\install.py", line 539, in run
        self.run_command('build')
      File "c:\python\python36\lib\distutils\cmd.py", line 313, in run_command
        self.distribution.run_command(command)
      File "c:\python\python36\lib\distutils\dist.py", line 974, in run_command
        cmd_obj.run()
      File "c:\python\python36\lib\distutils\command\build.py", line 135, in run
        self.run_command(cmd_name)
      File "c:\python\python36\lib\distutils\cmd.py", line 313, in run_command
        self.distribution.run_command(command)
      File "c:\python\python36\lib\distutils\dist.py", line 974, in run_command
        cmd_obj.run()
      File "c:\python\python36\lib\distutils\command\build_ext.py", line 338, in run
        self.build_extensions()
      File "C:\Users\ABDULR~1\AppData\Local\Temp\pip-build-rez5zpri\Pillow\setup.py", line 521, in build_extensions
        ' using --disable-%s, aborting' % (f, f))
    ValueError: zlib is required unless explicitly disabled using --disable-zlib, aborting

    ----------------------------------------
Command "c:\python\python36\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\ABDULR~1\\AppData\\Local\\Temp\\pip-build-rez5zpri\\Pillow\\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record C:\Users\ABDULR~1\AppData\Local\Temp\pip-a5bugnjo-record\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:\Users\ABDULR~1\AppData\Local\Temp\pip-build-rez5zpri\Pillow\

不知道要添加参数--disable-zlibpip install Pillow --disable-zlib是不正确的。

我在这里找不到适合我的系统的版本:https://pypi.python.org/pypi/Pillow/3.0.0

我的系统是64位Windows 10和Python 3.6。

5个回答

92

对于Python 2.x版本,您可以简单地使用以下命令:

  • pip install pillow

但是对于Python 3.X版本,您需要指定以下命令:

  • (sudo) pip3 install pillow

在Bash中输入pip并按tab键,您将看到可用的选项。


我使用 pip3 install pillow 命令时遇到了关于缺少 zlib 的相同错误。我该如何在命令中添加 --disable-zlib?或者更好的方法是安装 zlib。我不确定我的脚本是否需要 zlib。 - Abdulrahman Hassoun
1
这并不是针对特定问题的答案。从问题中可以清楚地看出,OP正在使用Python 3.6下的pip。 - arokem
pip install pillow 在 Python 3.5 上对我有效。所以答案并不是很到位。一个原因可能是进行 pip 升级,然后尝试 pip install --upgrade pip 并尝试安装 pillow 模块。 - Ganesh S
代码的统一化处理从pip库中可以很好地实现。如果您使用'pip'命令,无需指定环境是否为3.X或2.X。对于两个版本,pip命令都可以正常工作。 - slideWXY
我已经使用Python编程约2年了,一直使用Python 3。我发誓从未使用过“pip3”。 - KDM
在Windows 10上,对于Python 3.x,您需要以管理员身份打开并运行CMD提示符,然后再使用您所需的Python 3.x版本运行pip3 install pillow。这是为那些安装了多个Python版本的用户准备的。 - Joseph

5
您可以在这里下载与您配置相对应的包(在您的情况下是"Pillow‑4.1.1‑cp36‑cp36m‑win_amd64.whl"),并使用以下命令安装:pip install some-package.whl。如果您无法安装该包,请参考此答案

2
Pillow发布了Windows安装包:
我们为Windows提供Pillow二进制文件,编译支持矩阵的Python 32位和64位版本,包括wheel、egg和可执行安装程序。这些二进制文件都包含所有可选库。
更新:Pillow现在支持Python 3.6。使用pip install pillow进行安装,并查看https://pillow.readthedocs.io/en/latest/installation.html获取更多信息。
然而,Python 3.6仍处于alpha测试阶段,尚未正式支持,但夜间Python构建(当前为3.6a4)的所有测试都通过了。
如果可以在Python 3.6上安装3.5 wheel,则最好使用此方法。否则,除zlib之外,您需要从源代码构建,需要MS Visual C++编译器,而且不是很简单。有关提示,请参见:https://pillow.readthedocs.io/en/3.3.x/installation.html#building-from-source 还可以查看AppVeyor CI上的Windows构建方式(但尚未支持3.5或3.6):https://github.com/python-pillow/Pillow/tree/master/winbuild
如果以上方法均无法解决问题,请降级到Python 3.5或等待Pillow支持3.6,可能会在3.6正式发布时推出。

0
你可能需要检查版本:
  • pillow==4.0.0 添加了对Python 3.6的支持(source
  • pillow==9.0.0 不再支持Python 3.6(source

所以最新版本是pillow 8.4.0

pip install pillow==8.4.0

额外的依赖项

你可能会看到类似这样的信息:

main.RequiredDependencyException: jpeg

那么你需要安装额外的依赖项:https://pillow.readthedocs.io/en/stable/installation.html

在我的Ubuntu系统上,我需要执行以下命令:

sudo apt install libjpeg-dev zlib1g-dev

根据https://dev59.com/5lsW5IYBdhLWcg3wwZUP#34631976 这也可能适用于你的情况。


0
在Windows 10上,如果你在PyCharm上编程时遇到了安装PIL/pillow的困难,并且使用的是Python 3.10.6,请按照以下步骤进行:
  1. 关闭 PyCharm IDE,打开并以管理员身份运行CMD.exe提示符
  2. CMD提示符中,运行pip3 install pillow
  3. 安装完pillow后,关闭CMD提示符
  4. 打开PyCharm IDE,你会发现现在可以导入PIL

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