pyenv 配置:错误:C 编译器无法创建可执行文件

jes*_*son 14 python macos gcc pyenv macos-big-sur

我正在尝试在运行 MacOS Big Sur v11.1 的笔记本电脑上安装多个版本的 Python。我最初通过此处的说明安装了 xcode 命令行工具、自制软件和 python

xcode-select --install

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

brew install python

但是后来不由自主地将python升级到3.9,我需要回到3.7.9。所以我安装了 pyenv 并尝试按照此处的说明安装 python 3.7.9

brew install pyenv

pyenv install 3.7.9

但是我遇到了以下错误:

/var/folders/8n/ml0qwc091w9bhpszzxy9djl00000gn/T/python-build.20210118111111.56108 /usr/local/Cellar
/var/folders/8n/ml0qwc091w9bhpszzxy9djl00000gn/T/python-build.20210118111111.56108/Python-3.7.9 /var/folders/8n/ml0qwc091w9bhpszzxy9djl00000gn/T/python-build.20210118111111.56108 /usr/local/Cellar
checking build system type... x86_64-apple-darwin20.2.0
checking host system type... x86_64-apple-darwin20.2.0
checking for python3.7... no
checking for python3... python3
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... checking for --without-gcc... no
checking for --with-icc... no
checking for gcc... clang
checking whether the C compiler works... no
configure: error: in `/var/folders/8n/ml0qwc091w9bhpszzxy9djl00000gn/T/python-build.20210118111111.56108/Python-3.7.9':
configure: error: C compiler cannot create executables
See `config.log' for more details
make: *** No targets specified and no makefile found.  Stop.
Run Code Online (Sandbox Code Playgroud)

从网上的其他答案看来,我的 gcc 可能已经过时了。我检查了我的 gcc 版本

[/usr/local/Cellar]$ gcc --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr
    --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1 Apple clang version 11.0.3 (clang-1103.0.32.62) Target: x86_64-apple-darwin20.2.0 Thread model: posix InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Run Code Online (Sandbox Code Playgroud)

这似乎表明我使用的是 4.2.1 版。我还没有找到一种方法来自己更新它,但这样brew install gcc做并没有解决问题。任何帮助将非常感激

编辑: 似乎 pyenv 正在使用 clang 而不是 gcc。也许 xcode-select 都安装了?clang --version返回:

Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin20.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Run Code Online (Sandbox Code Playgroud)

小智 14

这对我有用:我以 root 身份删除了命令行工具并重新安装了......

您的命令行工具 (CLT) 不支持 macOS 11。它已过时或已被修改。如果没有可用更新,请更新您的命令行工具 (CLT) 或将其删除。

从系统偏好设置中的软件更新更新它们或运行:

softwareupdate --all --install --force
Run Code Online (Sandbox Code Playgroud)

如果这没有显示任何更新,请运行:

sudo rm -rf /Library/Developer/CommandLineTools
sudo xcode-select --install
Run Code Online (Sandbox Code Playgroud)

或者,从以下位置手动下载它们:

https://developer.apple.com/download/more/

  • 根本没用。错误仍然显示给我。 (3认同)

小智 14

我使用了以下内容并且有效

CC=gcc pyenv 安装 3.7.10


小智 10

Xcode、zlib 和编译器选项似乎有问题。他们从brew 为 3.8.0 python 版本做了一些补丁。

我的配置

$ sw_vers
ProductName:    macOS
ProductVersion: 11.2.3
BuildVersion:   20D91
Run Code Online (Sandbox Code Playgroud)
$ clang --version
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: x86_64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Run Code Online (Sandbox Code Playgroud)

这对我安装 3.8.3 和 3.9.0 有用


# Re-install Xcode
sudo rm -rf /Library/Developer/CommandLineTools
xcode-select --install

# Install zlib and bzip2 using brew
brew reinstall zlib bzip2
 
# Install tcl-tk tkinter
brew install tcl-tk

# Uninstall previous versions from python
pyenv uninstall 3.8.3
pyenv uninstall 3.9.0

# Install python 3.8.3 patched
env \
  PATH="$(brew --prefix tcl-tk)/bin:$PATH" \
  LDFLAGS="-L$(brew --prefix tcl-tk)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib" \
  CPPFLAGS="-I$(brew --prefix tcl-tk)/include -L$(brew --prefix zlib)/include -L$(brew --prefix bzip2)/include" \
  PKG_CONFIG_PATH="$(brew --prefix tcl-tk)/lib/pkgconfig" \
  CFLAGS="-I$(brew --prefix tcl-tk)/include -I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix zlib)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include" \
  LDFLAGS="-I$(brew --prefix tcl-tk)/lib -L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib"
  pyenv install --patch 3.8.3 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)
  
env \
  PATH="$(brew --prefix tcl-tk)/bin:$PATH" \
  LDFLAGS="-L$(brew --prefix tcl-tk)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib" \
  CPPFLAGS="-I$(brew --prefix tcl-tk)/include -L$(brew --prefix zlib)/include -L$(brew --prefix bzip2)/include" \
  PKG_CONFIG_PATH="$(brew --prefix tcl-tk)/lib/pkgconfig" \
  CFLAGS="-I$(brew --prefix tcl-tk)/include -I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix zlib)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include" \
  LDFLAGS="-I$(brew --prefix tcl-tk)/lib -L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib"
  pyenv install 3.9.0

Run Code Online (Sandbox Code Playgroud)

我确实提出了解决方案的消息来源: