如何为 Python 3.6 安装 PIL/Pillow?

Posted

技术标签:

【中文标题】如何为 Python 3.6 安装 PIL/Pillow?【英文标题】:How do I install PIL/Pillow for Python 3.6? 【发布时间】:2017-01-03 22:22:23 【问题描述】:

我有一个需要 PIL 才能运行的脚本。除了降级我的 Python,我找不到在我的 Python 3.6 上安装 PIL

这是我的尝试:

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

【问题讨论】:

【参考方案1】:

对于 python 版本 2.x,您可以简单地使用

pip install pillow

但是对于 python 3.X 版本你需要指定

(sudo) pip3 install pillow

当您在 bash hit tab 中输入 pip 时,您会看到您有哪些选项

【讨论】:

我收到与pip3 install pillow 相同的错误(关于缺少 zlib)。如何将 --disable-zlib 添加到命令中?或者最好安装 zlib。我不确定我的脚本是否需要 zlib 这并不是这个特定问题的真正答案。从问题中可以清楚地看出,OP 在 Python 3.6 下使用 pip。 pip install 枕头适用于我在 python 3.5 上。所以答案真的不是重点。一个原因可能是进行 pip 升级和 try.pip install --upgrade pip 然后尝试安装枕头模块 pip 库中代码的统一工作正常。您无需指定您的环境是在 3.X 中还是在 2.X 中。如果您使用 'pip' 命令,pip 命令对两者都适用 我已经在 python 中编程了大约 2 年;总是在 Python 3 中。我发誓我从未使用过“pip3”。【参考方案2】:

您可以下载与您的配置相对应的*** here(在您的情况下为“Pillow‑4.1.1‑cp36‑cp36m‑win_amd64.whl”)并安装它:

pip install some-package.whl

如果安装***有问题请阅读this answer

【讨论】:

【参考方案3】:

Pillow 在 Windows 上随安装轮发布:

我们为 Windows 提供 Pillow 二进制文件 为支持的 Python 矩阵编译 在 32 位和 64 位版本中,车轮、鸡蛋、 和可执行的安装程序。这些二进制文件有 包括所有可选库

https://pillow.readthedocs.io/en/3.3.x/installation.html#basic-installation

更新:Pillow 现在支持 Python 3.6。使用pip install pillow 安装并查看https://pillow.readthedocs.io/en/latest/installation.html 了解更多信息。


然而,Python 3.6 仍处于 alpha 阶段,尚未得到官方支持,尽管对于夜间 Python 构建(当前为 3.6a4)的测试都通过了。

https://travis-ci.org/python-pillow/Pillow/jobs/155605577

如果可以以某种方式为 3.6 安装 3.5 轮,那是您最好的选择。否则,尽管有 zlib,您仍需要从源代码构建,需要 MS Visual C++ 编译器,这并不简单。有关提示,请参阅:

https://pillow.readthedocs.io/en/3.3.x/installation.html#building-from-source

还可以看看它是如何在 AppVeyor CI(但还不是 3.5 或 3.6)上为 Windows 构建的:

https://github.com/python-pillow/Pillow/tree/master/winbuild

如果做不到,降级到 Python 3.5 或等到 Pillow 支持 3.6,可能更接近 3.6 的正式版本。

【讨论】:

以上是关于如何为 Python 3.6 安装 PIL/Pillow?的主要内容,如果未能解决你的问题,请参考以下文章

Python图片识别——人工智能篇

Python图片识别——人工智能篇

Python图片识别——人工智能篇

我如何为不和谐的机器人 python 3.6 定义 MissingPermissions

图像旋转任意角度+python

PIL pip error