Mac Pillow
Environment:macOS Mojave
Version:10.14.2
Time:2019.03.01
PIL是python的一个图形库,而最新的一个版本是2009年为1.1.7更新的,虽然这么久远了,但是官方还未说明PIL项目被抛弃。而Pillow是fork PIL项目的,Pillow的作者的原意也是为了促进以及支持PIL的积极开发。
macOS 默认自带python2.7的版本,而我的项目是基于python3进行的,所以我装了一个python3,版本是3.7.2的,这里要做的是导下Pillow项目,然后修改了一些代码,然后替换掉系统的PIL。
Note
Pillow支持的python版本
Python | 2.4 | 2.5 | 2.6 | 2.7 | 3.2 | 3.3 | 3.4 | 3.5 | 3.6 | 3.7 |
---|---|---|---|---|---|---|---|---|---|---|
Pillow < 2.0.0 | Yes | Yes | Yes | Yes | ||||||
Pillow 2.x - 3.x | Yes | Yes | Yes | Yes | Yes | Yes | ||||
Pillow 4.x | Yes | Yes | Yes | Yes | Yes | |||||
Pillow 5.0.x - 5.1.x | Yes | Yes | Yes | Yes | ||||||
Pillow >= 5.2.0 | Yes | Yes | Yes | Yes | Yes |
注:PIL和Pillow在相同的环境下会冲突,所以在安装Pillow之前,请先卸载PIL。
预备工作
首先安装python3
其次在GitHub上面导下Pillow项目,在本地进行修改。
git clone https://github.com/python-pillow/Pillow.git MyPillow
然后卸载掉自己电脑上面的PIL(python3使用pip3)
pip3 uninstall PIL
安装Pillow需要用到Xcode的命令行工具,所以需要先执行
xcode-select --install
执行命令行工具之前可能还要接受许可:
sudo xcodebuild -license
你还需要通过Homebrew安装一些Pillow需要用到的依赖库:
brew install libtiff libjpeg webp little-cms2
正式工作
安装本地Pillow项目。进入MyPillow目录,并执行安装命令,
pip3 install .
或者通过在未压缩的项目目录下执行安装:
python3 setup.py install
好了,现在你的电脑就已经成功装上Pillow了
问题
我这里会出现一个问题,就是执行安装的时候会出现zlib模块缺少,报错如下:
The headers or library files could not be found for zlib,
a required dependency when compiling Pillow from source.
Please see the install instructions at:
https://pillow.readthedocs.io/en/latest/installation.html
Traceback (most recent call last):
File "setup.py", line 803, in <module>
zip_safe=not (debug_build() or PLATFORM_MINGW), )
File "/usr/local/lib/python3.7/site-packages/setuptools/__init__.py", line 143, in setup
return distutils.core.setup(**attrs)
File "/usr/local/Cellar/python/3.7.2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/core.py", line 148, in setup
dist.run_commands()
File "/usr/local/Cellar/python/3.7.2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/dist.py", line 966, in run_commands
self.run_command(cmd)
File "/usr/local/Cellar/python/3.7.2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/usr/local/lib/python3.7/site-packages/setuptools/command/install.py", line 67, in run
self.do_egg_install()
File "/usr/local/lib/python3.7/site-packages/setuptools/command/install.py", line 109, in do_egg_install
self.run_command('bdist_egg')
File "/usr/local/Cellar/python/3.7.2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/usr/local/Cellar/python/3.7.2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/usr/local/lib/python3.7/site-packages/setuptools/command/bdist_egg.py", line 172, in run
cmd = self.call_command('install_lib', warn_dir=0)
File "/usr/local/lib/python3.7/site-packages/setuptools/command/bdist_egg.py", line 158, in call_command
self.run_command(cmdname)
File "/usr/local/Cellar/python/3.7.2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/usr/local/Cellar/python/3.7.2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/usr/local/lib/python3.7/site-packages/setuptools/command/install_lib.py", line 11, in run
self.build()
File "/usr/local/Cellar/python/3.7.2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/command/install_lib.py", line 107, in build
self.run_command('build_ext')
File "/usr/local/Cellar/python/3.7.2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/usr/local/Cellar/python/3.7.2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/usr/local/Cellar/python/3.7.2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/command/build_ext.py", line 339, in run
self.build_extensions()
File "setup.py", line 590, in build_extensions
raise RequiredDependencyException(f)
__main__.RequiredDependencyException: zlib
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "setup.py", line 815, in <module>
raise RequiredDependencyException(msg)
__main__.RequiredDependencyException:
The headers or library files could not be found for zlib,
a required dependency when compiling Pillow from source.
Please see the install instructions at:
https://pillow.readthedocs.io/en/latest/installation.html
在全世界Google搜索了几遍之后,尝试了N多次方法之后,还是云里雾里的。在不懈的搜索中,尝试了一下某个帖子的方法,结果还真解决了,想哭。帖子地址:https://github.com/pyenv/pyenv/issues/1219
只要执行以下命令,然后重新安装就成功了。
sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /
笔记,终。