写了很久的Python2,今天被Python3 *等特性吸引了,忍不住想试一下。

在MAC OS X上Python的多版本共存,我们可以使用pyenv来解决,配合virtualenv,可以轻松搭建多个虚拟环境。

但是安装的时候,遇到了两个问题。

pyenv错误提示

ZSH

1

2

3

4

~ pyenv install 3.5-dev

Cloning https://hg.python.org/cpython...

BUILD FAILED (OS X 10.11.6 using python-build 20150818)

在官网的issues#429发现,用下面的方式解决。

ZSH

1

2

~ brew link --force openssl

Linking /usr/local/Cellar/openssl/1.0.2d_1... 1548 symlinks created

然后又遇到了下面的问题:

第二个问题

ZSH

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

~ pyenv install 3.5-dev

Cloning https://hg.python.org/cpython...

Installing Python-3.5-dev...

BUILD FAILED (OS X 10.11.6 using python-build 20150818)

Inspect or clean up the working tree at /var/folders/23/4kbs9t712jv1mvmw6cpjwr2m0000gn/T/python-build.20160815000934.22185

Results logged to /var/folders/23/4kbs9t712jv1mvmw6cpjwr2m0000gn/T/python-build.20160815000934.22185.log

Last 10 log lines:

  File "/private/var/folders/23/4kbs9t712jv1mvmw6cpjwr2m0000gn/T/python-build.20160815000934.22185/Python-3.5-dev/Lib/ensurepip/__main__.py", line 4, in <module>

    ensurepip._main()

  File "/private/var/folders/23/4kbs9t712jv1mvmw6cpjwr2m0000gn/T/python-build.20160815000934.22185/Python-3.5-dev/Lib/ensurepip/__init__.py", line 209, in _main

    default_pip=args.default_pip,

  File "/private/var/folders/23/4kbs9t712jv1mvmw6cpjwr2m0000gn/T/python-build.20160815000934.22185/Python-3.5-dev/Lib/ensurepip/__init__.py", line 116, in bootstrap

    _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)

  File "/private/var/folders/23/4kbs9t712jv1mvmw6cpjwr2m0000gn/T/python-build.20160815000934.22185/Python-3.5-dev/Lib/ensurepip/__init__.py", line 40, in _run_pip

    import pip

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

make: *** [install] Error 1

然后又查到了issue#451,应该使用下面的命令来安装。

安装命令

ZSH

1

2

3

~ CFLAGS="-I$(brew --prefix openssl)/include -I$(xcrun --show-sdk-path)/usr/include" \

  LDFLAGS="-L$(brew --prefix openssl)/lib" \

  pyenv install -v 3.5-dev

然后,就安装成功了。

安装成功

ZSH

1

2

3

~ pyenv versions

* system (set by /Users/laixintao/.pyenv/version)

  3.5-dev


如果需要使用虚拟环境,可以使用下面的命令安装pyenv-virtualenv插件,然后新建虚拟环境。

pyenv-virtualenv

ZSH

1

2

3

4

5

6

7

8

9

10

11

12

13

~ git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv

Cloning into '/Users/laixintao/.pyenv/plugins/pyenv-virtualenv'...

remote: Counting objects: 1849, done.

remote: Total 1849 (delta 0), reused 0 (delta 0), pack-reused 1849

Receiving objects: 100% (1849/1849), 527.44 KiB | 699.00 KiB/s, done.

Resolving deltas: 100% (1267/1267), done.

Checking connectivity... done.

~ echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile

~ source ~/.bash_profile

~ pyenv virtualenv 3.5-dev hackerrank

Ignoring indexes: https://pypi.python.org/simple

Requirement already satisfied (use --upgrade to upgrade): setuptools in /Users/laixintao/.pyenv/versions/3.5-dev/envs/hackerrank/lib/python3.5/site-packages

Requirement already satisfied (use --upgrade to upgrade): pip in /Users/laixintao/.pyenv/versions/3.5-dev/envs/hackerrank/lib/python3.5/site-packages

以这样的方式新建的虚拟环境,位置在~/.pyenv/versions如果使用pycharm的话,可以从这个路径导入。