pyenv安装python问题解决

mac升级系统后pyenv安装python问题

  • 以安装3.6.8为例
    执行pyenv install 3.6.8如下报错
pyenv install 3.6.8
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.6.8.tar.xz...
-> https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tar.xz
Installing Python-3.6.8...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk

BUILD FAILED (OS X 10.16 using python-build 1.2.21-1-g943015eb)

Inspect or clean up the working tree at /var/folders/m6/z1s97vbs7xd4wzf6qcnm5xw80000gn/T/python-build.20201204110013.67925
Results logged to /var/folders/m6/z1s97vbs7xd4wzf6qcnm5xw80000gn/T/python-build.20201204110013.67925.log

Last 10 log lines:
ssize_t pwritev(int, const struct iovec *, int, off_t) __DARWIN_NOCANCEL(pwritev) __API_AVAILABLE(macos(11.0), ios(14.0), watchos(7.0), tvos(14.0));
        ^
./Modules/posixmodule.c:8805:18: note: enclose 'pwritev' in a __builtin_available check to silence this warning
        result = pwritev(fd, iov, cnt, offset);
                 ^~~~~~~
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include   -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include   -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration   -I. -I./Include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/Users/tina/.pyenv/versions/3.6.8/include  -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/Users/tina/.pyenv/versions/3.6.8/include    -c ./Modules/errnomodule.c -o Modules/errnomodule.o
2 warnings and 1 error generated.
make: *** [Modules/posixmodule.o] Error 1
make: *** Waiting for unfinished jobs....
1 warning generated.
  • 采用如下方式安装
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 3.6.8 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)

依然报错:

Last 10 log lines:
  File "/private/var/folders/m6/z1s97vbs7xd4wzf6qcnm5xw80000gn/T/python-build.20201204104319.35832/Python-3.6.8/Lib/ensurepip/__main__.py", line 5, in <module>
    sys.exit(ensurepip._main())
  File "/private/var/folders/m6/z1s97vbs7xd4wzf6qcnm5xw80000gn/T/python-build.20201204104319.35832/Python-3.6.8/Lib/ensurepip/__init__.py", line 204, in _main
    default_pip=args.default_pip,
  File "/private/var/folders/m6/z1s97vbs7xd4wzf6qcnm5xw80000gn/T/python-build.20201204104319.35832/Python-3.6.8/Lib/ensurepip/__init__.py", line 117, in _bootstrap
    return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
  File "/private/var/folders/m6/z1s97vbs7xd4wzf6qcnm5xw80000gn/T/python-build.20201204104319.35832/Python-3.6.8/Lib/ensurepip/__init__.py", line 27, in _run_pip
    import pip._internal
zipimport.ZipImportError: can't decompress data; zlib not available
  • 重新安装zlib
brew uninstall --force zlib
brew cleanup -s zlib
brew cleanup --prune-prefix
brew install zlib

添加环境变量

export LDFLAGS="-L/usr/local/opt/zlib/lib"
  • 安装成功
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 3.6.8 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Installing Python-3.6.8...
patching file Misc/NEWS.d/next/macOS/2020-06-24-13-51-57.bpo-41100.mcHdc5.rst
patching file configure
Hunk #1 succeeded at 3375 (offset -51 lines).
patching file configure.ac
Hunk #1 succeeded at 495 (offset -15 lines).
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
Installed Python-3.6.8 to /Users/tina/.pyenv/versions/3.6.8

参考