解決 MacBook 上編譯 Python 3.8.6 失敗問題

zlib 模組無法找到

小前言

在 MacBook 上要使用 Python 3.8.6 時遇到兩個問題:

Python 中匯入 Pandas 失敗
編譯 Python 3.8.6 錯誤訊息

MacBook 版本

$ sw_vers
ProductName: macOS
ProductVersion: 11.0.1
BuildVersion: 20B29

前置需求

  1. 已裝有 pyenv
  2. 已透過 brew 裝有 zlibbzip2opensslopenblas 、和 readline

解決方法

安裝最新版 Xcode Command Line Tools

  1. 下載 Xcode Command Line Tools 12.2 蘋果磁碟映像檔,然後點擊下載。
    https://download.developer.apple.com/Developer_Tools/Command_Line_Tools_for_Xcode_12.2/Command_Line_Tools_for_Xcode_12.2.dmg
    或是透過命令列指令進行下載。
    xcode-select --install
結合環境變數指定套件透過 pyenv 安裝 Python
$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && \
python3 get-pip.py && \
rm get-pip.py
$ python3 -m pip --use-feature=2020-resolver \
--no-cache-dir install numpy==1.18.0
$ python3 -m pip --use-feature=2020-resolver \
--no-cache-dir install pandas
> python3[Clang 12.0.0 (clang-1200.0.32.27)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> import pandas as pd
>>> numpy.__version__
'1.18.0'
>>> pandas.__version__
'1.1.4'

後語

不要不信邪啊,想用 1.19.x 的 numpy 還不是時候啊,不然你就是透過 brew 安裝 python 和 numpy 就能在 MacBook 上使用 1.19.x 的 numpy 啦!指定openblas 透過 pip 安裝最新版的 numpy 也是沒有法子的,好嗎?

參考

  1. Developer.apple.com. n.d. Technical Note TN2339: Building From The Command Line With Xcode FAQ. [online] Available at: <https://developer.apple.com/library/archive/technotes/tn2339/_index.html#//apple_ref/doc/uid/DTS40014588-CH1-HOW_CAN_I_UNINSTALL_THE_COMMAND_LINE_TOOLS_> [Accessed 20 November 2020].
  2. Friend, J., 2020. Pyenv/Pyenv. [online] GitHub. Available at: <https://github.com/pyenv/pyenv/wiki/Common-build-problems> [Accessed 20 November 2020].
  3. Gedam, P., 2020. Announcement: Pip 20.2 Release!. [online] Discussions on Python.org. Available at: <https://discuss.python.org/t/announcement-pip-20-2-release/4863> [Accessed 20 November 2020].
  4. glyph, 2020. Buggy Accelerate Backend When Using Numpy 1.19 · Issue #15947 · Numpy/Numpy. [online] GitHub. Available at: <https://github.com/numpy/numpy/issues/15947> [Accessed 20 November 2020].
  5. snowcat, 2019. Command Line Tools — Headers Missi… | Apple Developer Forums. [online] Developer.apple.com. Available at: <https://developer.apple.com/forums/thread/122762> [Accessed 20 November 2020].

7 x AWS-certified Sr. data engineer in the 104 Corporation. An AWS Community Builder