网上有使用pyenv
方式安装python3以实现与系统的python版本共存而不冲突,我的以为其实没有必要,咱们其实能够单独运行python3。
首先咱们仍是须要先安装python3,这里使用homebrew安装,方便快捷好管理,棒棒哒python
brew install python3
安装好后能够尝试输入python3看是否能进入python3命令行,能够看到我这里安装的python3的版本是3.5.2shell
$ python3 Python 3.5.2 (default, Jun 29 2016, 13:43:58) [GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
作为pythoneer,不得不知的三大神器:virtualenv
, fabric
, pip
,今天的主角就是其中之一vrtualenv
,那么它是拿来作什么的呢,其实看意思也能懂咯,就是一个python的虚拟环境,中文也叫虚拟沙盒,就是说它能把项目放在一个虚拟的环境里边,在这个环境里你使用的python版本以及安装的依赖都不会影响环境外的项目。python2.7
$ pip install virtualenv
virtualenv 环境名称[自定义] 参数
参数:
--no-site-packages package //不依赖已经装好的第三方package,默认会依赖
能够经过virtualenv --help
查看更多其它参数,话很少说,先撸代码命令行
wwwuser@iZ28u3wd0b6Z:~$ virtualenv test_env New python executable in /home/wwwuser/test_env/bin/python Installing setuptools, pip, wheel...done. wwwuser@iZ28u3wd0b6Z:~$
完成后在当前目录会建立一个test_env
的文件夹,进入文件夹会发现生成了如下的目录,神奇吧code
├── bin ├── include │ └── python2.7 ├── lib │ └── python2.7 //全部的新包会被存在这 │ ├── distutils │ ├── encodings │ ├── lib-dynload │ └── site-packages ├── local │ ├── bin │ ├── include │ └── lib
wwwuser@iZ28u3wd0b6Z:~/test_env$ source ./bin/activate (test_env) wwwuser@iZ28u3wd0b6Z:~/test_env$
启动成功后,会在前面多出test_env字样
输入pip list
查看项目依赖orm
(test_env) wwwuser@iZ28u3wd0b6Z:~/test_env$ pip list pip (8.0.2) setuptools (19.6.1) wheel (0.26.0)
能够发现沙箱确实已是一个单独的环境了homebrew
deactivate
使用--python
参数指定python版本建立一个基于python3的虚拟环境ip
virtualenv py3_test --python=3.5
检查环境中python版本,能够发现虚拟环境中的python版本已是python3啦,好啦,这样即大功告成!it
$ cd py3_test tianyu at whitneydeMacBook-Pro in ~/Work/py3_test (py3_test) $ python Python 3.5.2 (default, Jun 29 2016, 13:43:58) [GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>