python3感觉用虚拟环境会比较好操作一些,也不用直接卸载python2。
一、基于brew快速安装
- # 安装python3
- brew install python3
- # 安装pip(好像3自带pip,不用安装)
- sudo easy_install pip
- # 安装虚拟环境
- pip install virtualenv
基本操作:创建3的项目
- # 创建项目
- virtualenv py3_test --python=3.5
- # 进入项目文件夹
- cd py3_test
- # 激活虚拟环境
- source ./bin/activate
- # 此时在这个文件夹下已经为3的环境
- # 退出虚拟环境
- deactivate
二、通过源码安装
重点:SSL的配置;好像3默认自带pip;
- # 下载解压
- curl -OL http://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz
- tar xzvf Python-3.6.1.tgz
- cd Python-3.6.1
- # 安装openssl依赖
- brew install openssl
- # 显示出openssl的具体位置
- # brew --prefix openssl
- # /usr/local/opt/openssl
- # 配置
- ./configure CPPFLAGS="-I/usr/local/opt/openssl/include" LDFLAGS="-L/usr/local/opt/openssl/lib"
-
- # 编译并安装
- make
- make install
创建项目
- # 创建项目
- python3.6 -m venv py3_test
- # 进入项目文件夹
- cd py3_test
- # 激活虚拟环境
- source ./bin/activate
- # 此时在这个文件夹下已经为3的环境
三、通过上面的虚拟环境都有一个问题,每次进去文件夹都需要激活,那么可以通过下面的组件自动触发激活
安装:
- git clone git://github.com/kennethreitz/autoenv.git ~/.autoenv
- # 安装默认shell
- echo 'source ~/.autoenv/activate.sh' >> ~/.bashrc
- # 安装了zsh
- echo 'source ~/.autoenv/activate.sh' >> ~/.zshrc
使用:
- # 进入项目文件夹
- cd py3_test
- # 激活虚拟环境
- echo "source ./bin/activate" > .env
总结:
安装各有千秋,brew也行,源码也行,反正不用在生产环境上使用,只求能用就好。