Mac 10.14 下 pyenv 安装 No module named 'pyexpat'

利用pyenv 进行环境安装的时候遇到错误:

$ pyenv install 3.7.6
python-build: use [email protected] from homebrew
python-build: use readline from homebrew
Downloading Python-3.7.6.tar.xz...
-> http://mirrors.sohu.com/python/3.7.6/Python-3.7.6.tar.xz
Installing Python-3.7.6...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk

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

Inspect or clean up the working tree at /var/folders/jn/sv_y0c6d12q7kzjrhb_sjd_c0000gn/T/python-build.20200208172322.3019
Results logged to /var/folders/jn/sv_y0c6d12q7kzjrhb_sjd_c0000gn/T/python-build.20200208172322.3019.log

Last 10 log lines:
  File "/var/folders/jn/sv_y0c6d12q7kzjrhb_sjd_c0000gn/T/tmp8r0lmo8a/pip-19.2.3-py2.py3-none-any.whl/pip/_internal/cli/cmdoptions.py", line 24, in 
  File "/var/folders/jn/sv_y0c6d12q7kzjrhb_sjd_c0000gn/T/tmp8r0lmo8a/pip-19.2.3-py2.py3-none-any.whl/pip/_internal/models/search_scope.py", line 11, in 
  File "/var/folders/jn/sv_y0c6d12q7kzjrhb_sjd_c0000gn/T/tmp8r0lmo8a/pip-19.2.3-py2.py3-none-any.whl/pip/_internal/utils/misc.py", line 21, in 
  File "/var/folders/jn/sv_y0c6d12q7kzjrhb_sjd_c0000gn/T/tmp8r0lmo8a/pip-19.2.3-py2.py3-none-any.whl/pip/_vendor/pkg_resources/__init__.py", line 35, in 
  File "/private/var/folders/jn/sv_y0c6d12q7kzjrhb_sjd_c0000gn/T/python-build.20200208172322.3019/Python-3.7.6/Lib/plistlib.py", line 65, in 
from xml.parsers.expat import ParserCreate
  File "/private/var/folders/jn/sv_y0c6d12q7kzjrhb_sjd_c0000gn/T/python-build.20200208172322.3019/Python-3.7.6/Lib/xml/parsers/expat.py", line 4, in 
from pyexpat import *
ModuleNotFoundError: No module named 'pyexpat'

解决问题方法:

SDKROOT=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk MACOSX_DEPLOYMENT_TARGET=10.14 pyenv install ...

2020-02-24更新:
第一次使用 pyenv-virtualenv 的是很可能遇到以下错误:

Failed to deactivate virtualenv.

Perhaps pyenv-virtualenv has not been loaded into your shell properly.
Please restart current shell and try again.

这个时候我们需要在自己的 .bash_profile 或是 .zshrc
追加以下两句设置:

 if which pyenv-virtualenv-init > /dev/null; then
   eval "$(pyenv init -)"
   eval "$(pyenv virtualenv-init -)";
fi

生效配置文件: source ~/.zshrc 就可以正常使用。

你可能感兴趣的:(Mac 10.14 下 pyenv 安装 No module named 'pyexpat')