macOS Pillow Build

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 /

筆記,終。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章