记录Mac下安装pyenv时所遇到的问题


虽然在我使用mac安装pyenv时遇到的问题与原博文的不完全一样,但是该博文中的很多解决思路使我解决了安装过程中的所有问题,为支持原文,所以,在此标注参考博文地址,并标明了该博文为转载。【原博文地址】


pyenv,是用来管理电脑不同版本的python的一个管理工具,切换不同版本不会影响到系统自带的python。
pyenv,是一个开源软件,具体可以猛戳这里:https://github.com/yyuu/pyenv
安装就不说了,可以看项目使用说明,我说说我遇到的问题。
我按照说明做,当我执行:

$ pyenv install 3.4.2

pyenv安装信息如下

Downloading Python-3.4.2.tgz...
-> https://yyuu.github.io/pythons/44a3c1ef1c7ca3e4fd25242af80ed72da941203cb4ed1a8c1b724d9078965dd8
error: failed to download Python-3.4.2.tgz
-> https://www.python.org/ftp/python/3.4.2/Python-3.4.2.tgz
Installing Python-3.4.2...
ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib?

Please consult to the Wiki page to fix the problem.
https://github.com/yyuu/pyenv/wiki/Common-build-problems

BUILD FAILED (OS X 10.9.5 using python-build 20150124)

Inspect or clean up the working tree at /var/folders/zf/1b6kcyd53hg0crv65j108_7m0000gn/T/python-build.20150127145029.76064
Results logged to /var/folders/zf/1b6kcyd53hg0crv65j108_7m0000gn/T/python-build.20150127145029.76064.log

Last 10 log lines:
(cd /Users/karonchen/.pyenv/versions/3.4.2/share/man/man1; ln -s python3.4.1 python3.1)
if test "xupgrade" != "xno"  ; then \
        case upgrade in \
            upgrade) ensurepip="--upgrade" ;; \
            install|*) ensurepip="" ;; \
        esac; \
         ./python.exe -E -m ensurepip \
            $ensurepip --root=/ ; \
    fi
Ignoring ensurepip failure: pip 1.5.6 requires SSL/TLS

原因:

ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib?

所以,执行下面的代码可以解决这个问题:

CFLAGS="-I$(brew --prefix openssl)/include" \
LDFLAGS="-L$(brew --prefix openssl)/lib" \
pyenv install 3.4.2

详情可以看着里:https://github.com/yyuu/pyenv/issues/22
pyenv安装反馈如下:

Downloading Python-3.4.2.tgz...
-> https://yyuu.github.io/pythons/44a3c1ef1c7ca3e4fd25242af80ed72da941203cb4ed1a8c1b724d9078965dd8
Installing Python-3.4.2...

BUILD FAILED (OS X 10.9.5 using python-build 20150124)

Inspect or clean up the working tree at /var/folders/zf/1b6kcyd53hg0crv65j108_7m0000gn/T/python-build.20150127150918.87726
Results logged to /var/folders/zf/1b6kcyd53hg0crv65j108_7m0000gn/T/python-build.20150127150918.87726.log

Last 10 log lines:
  File "/private/var/folders/zf/1b6kcyd53hg0crv65j108_7m0000gn/T/python-build.20150127150918.87726/Python-3.4.2/Lib/ensurepip/__main__.py", line 4, in 
    ensurepip._main()
  File "/private/var/folders/zf/1b6kcyd53hg0crv65j108_7m0000gn/T/python-build.20150127150918.87726/Python-3.4.2/Lib/ensurepip/__init__.py", line 209, in _main
    default_pip=args.default_pip,
  File "/private/var/folders/zf/1b6kcyd53hg0crv65j108_7m0000gn/T/python-build.20150127150918.87726/Python-3.4.2/Lib/ensurepip/__init__.py", line 116, in bootstrap
    _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
  File "/private/var/folders/zf/1b6kcyd53hg0crv65j108_7m0000gn/T/python-build.20150127150918.87726/Python-3.4.2/Lib/ensurepip/__init__.py", line 40, in _run_pip
    import pip
zipimport.ZipImportError: can't decompress data; zlib not available
make: *** [install] Error 1

原因:

zipimport.ZipImportError: can't decompress data; zlib not available

执行下面的代码即可解决这个问题:

CFLAGS="-I$(xcrun --show-sdk-path)/usr/include" pyenv install -v 3.4.2

详情可以看着里:https://github.com/yyuu/pyenv/issues/25
以为这样就搞定了。。。可是:
当我在shell中输入

pyenv local 3.4.2 #设置机子使用的python为3.4.2版本

$ python --version
$ 2.7.8

发现python还是之前的版本,这又是哪里出问题。
查了半天,发现是shell configuration没配置。具体可以参考这里:https://github.com/yyuu/pyenv/issues/228
需要在.bash_profile的最下面加入:

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

and it worked!!

你可能感兴趣的:(闲谈)