写了很久的Python2,今天被Python3 *等特性吸引了,忍不住想试一下。
在MAC OS X上Python的多版本共存,我们可以使用pyenv来解决,配合virtualenv,可以轻松搭建多个虚拟环境。
但是安装的时候,遇到了两个问题。
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发现,用下面的方式解决。
1 2 |
~ brew link --force openssl Linking /usr/local/Cellar/openssl/1.0.2d_1... 1548 symlinks created |
然后又遇到了下面的问题:
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,应该使用下面的命令来安装。
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 |
然后,就安装成功了。
1 2 3 |
~ pyenv versions * system (set by /Users/laixintao/.pyenv/version) 3.5-dev |
如果需要使用虚拟环境,可以使用下面的命令安装pyenv-virtualenv
插件,然后新建虚拟环境。
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的话,可以从这个路径导入。
实际上,由于Unix的里面一切都是文件,所以只要不同Python他们所在的文件夹是不同的,他们本身就可以共存。
通过设定~/.zshrc的alias即可实现用不同的名字启动不同的Python。而且如果直接使用homebrew安装Python的话,它也可以自动为你处理好环境变量,并没有必要在OS X上使用pyenv.
是这样啊 ^ ^
因为之前我有过安装第二个Python把系统的链接、pip链接什么的搞乱了,还删错了一些东西,所以不敢自己配置了,全都交给虚拟环境去管理了。
我现在的习惯是,对每一个项目,就新建一个virtualenv ^ ^ 这样导出依赖的时候也比较方便
Pingback: 一些命令行工具的增强版 – [email protected]