pyenv install | Xcode 编译 python 出错

环境

Macbook Pro 2019, i7
macOS Sonoma 14.0
Apple clang version 15.0.0 (clang-1500.0.40.1)
Xcode 15.0
pyenv 2.3.30

复现

完整报错

python-build: use openssl from homebrew
python-build: use readline from homebrew
Installing Python-3.7.4...
python-build: use readline from homebrew
python-build: use ncurses from homebrew
python-build: use zlib from xcode sdk

BUILD FAILED (OS X 14.0 using python-build 20180424)

Inspect or clean up the working tree at /var/folders/6v/jspqbsdn1hv45_5ymb_5jhv80000gp/T/python-build.20231017235510.58421
Results logged to /var/folders/6v/jspqbsdn1hv45_5ymb_5jhv80000gp/T/python-build.20231017235510.58421.log

Last 10 log lines:
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall    -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wno-cast-function-type -Werror=implicit-function-declaration   -I. -I./Include -I/usr/local/opt/ncurses/include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/Users/pan/.pyenv/versions/3.7.4/include -I/usr/local/opt/zlib/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -I/usr/local/opt/ncurses/include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/Users/pan/.pyenv/versions/3.7.4/include -I/usr/local/opt/zlib/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include  -DPy_BUILD_CORE_BUILTIN  -c ./Modules/_codecsmodule.c -o Modules/_codecsmodule.o
./Modules/posixmodule.c:8433:15: error: call to undeclared function 'sendfile'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
        ret = sendfile(in, out, offset, &sbytes, &sf, flags);
              ^
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall    -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wno-cast-function-type -Werror=implicit-function-declaration   -I. -I./Include -I/usr/local/opt/ncurses/include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/Users/pan/.pyenv/versions/3.7.4/include -I/usr/local/opt/zlib/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -I/usr/local/opt/ncurses/include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/Users/pan/.pyenv/versions/3.7.4/include -I/usr/local/opt/zlib/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include  -DPy_BUILD_CORE_BUILTIN  -c ./Modules/_weakref.c -o Modules/_weakref.o
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall    -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wno-cast-function-type -Werror=implicit-function-declaration   -I. -I./Include -I/usr/local/opt/ncurses/include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/Users/pan/.pyenv/versions/3.7.4/include -I/usr/local/opt/zlib/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -I/usr/local/opt/ncurses/include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/Users/pan/.pyenv/versions/3.7.4/include -I/usr/local/opt/zlib/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include  -DPy_BUILD_CORE_BUILTIN  -DPy_BUILD_CORE -c ./Modules/_functoolsmodule.c -o Modules/_functoolsmodule.o
1 error generated.
make: *** [Modules/posixmodule.o] Error 1
make: *** Waiting for unfinished jobs....
1 warning generated

解决方案

Homebrew 团队提供了一个 patch,加上这个补丁后,xcode 可以正常编译,我这里封装了一个函数,执行 pyenv-install 3.7.4 的命令来安装 3.7.4 版本的 python

# Alias for pyenv installation
function pyenv-install() {
    if [ -f ~/.pyenv/versions/$1/bin/python ]; then
        echo "Python $1 already installed"
        return
    fi
    if [ ! -f ~/.pyenv/cache/Python-$1.tar.xz ]; then
        wget https://registry.npmmirror.com/-/binary/python/$1/Python-$1.tar.xz -P ~/.pyenv/cache/
    fi
    CFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include" 
    LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib"
    pyenv install --patch $1 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)
}

效果如下:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

p1s1lver

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值