關於pythonbrew的介紹:https://github.com/utahta/pythonbrew php
中文文檔:http://pythonbrew.readthedocs.org/en/latest/ python
選擇pythonbrew的緣由:
git
融合了virtualenv,建立隔離環境更方便快捷github
具備pyenv的所擁有的全部功能(我的感受更強悍)
sql
列出可安裝的 python 版本:pythonbrew list --know
django
安裝某個版本的 python : pythonbrew install 2.7.3
flask
刪除已安裝的某版本的 python : pythonbrew uninstall 2.7.3
bash
列出已安裝的 python 版本(當前使用的版本後會用星號標記): pythonbrew list
ide
使用某個版本的 python (僅當前終端窗口有效): pythonbrew use 2.7.3
工具
切換到某個版本的 python (一直有效): pythonbrew switch 2.7.3
清理陳舊的源碼目錄和檔案包: pythonbrew cleanup
升級到pythonbrew到最新版本: pythonbrew update
禁用pythonbrew(即切換回原始環境): pythonbrew off
建立python隔離環境(藉助virtualenv):
安裝腳本:
(fuck)[root@php ~]# cat pythonbrew.sh #!/bin/bash #Function: create pythonbrew env #Author: zhuima #Date: 2014-11-06 #Version: 0.1 # REVTAL=0 # import Functions . /etc/rc.d/init.d/functions # check network . /etc/sysconfig/network if [ $NETWORKING = 'no' ];then exit $REVTAL fi # install epel yum source function epel_install(){ if rpm --version &>/dev/null;then rpm -ivh http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm else exit $REVTAL print "please checking your yum configure!" fi } # install base packages function base_packages(){ if yum repolist &>/dev/null;then yum install yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel patch -y else exit $REVTAL print "please checking your yum configure!" fi } # install pip function pip_install(){ if yum repolist &>/dev/null;then yum install python-pip -y else exit $REVTAL print "please checking your yum configure!" fi } # install pythonbrew function pythonbrew_install(){ if pip -V &>/dev/null;then pip install pythonbrew pip install virtualenv else exit $REVTAL print "please checking your pip configure!" fi } # config pythonbrew env function pythonbrew_env(){ echo '[[ -s "$HOME/.pythonbrew/etc/bashrc" ]] && source "$HOME/.pythonbrew/etc/bashrc"' >>~/.bashrc . /usr/bin/pythonbrew_install && source ~/.bashrc } # install python 2.7.6 function python_install(){ if $HOME/.pythonbrew/bin/pythonbrew --version &>/dev/null;then $HOME/.pythonbrew/bin/pythonbrew install 2.7.6 else exit $REVTAL print "please checking your pyenv configure" fi } # install ipdb、ipython function install_ipython(){ if pip --version &>/dev/null;then pip install ipdb pip install ipython else yum install pip -y pip install ansible pip install ipython fi } while :;do cat << EOF +-------------------------------------------+ |一、Install epel_install | |二、Install base_packages | |三、Install pip_install | |四、Install pythonbrew_install | |五、Install pythonbrew_env | |六、Install python_install | |七、Install install_ipython | |八、One-Click Setup | |九、[Q|q|quit] to quit | +-------------------------------------------+ EOF read -p "select which one packages you want to install: " choice case $choice in 1) epel_install ;; 2) base_packages ;; 3) pip_install ;; 4) pythonbrew_install ;; 5) pythonbrew_env ;; 6) python_install ;; 7) install_ipython ;; 8) epel_install base_packages pip_install pythonbrew_install pythonbrew_env python_install install_ipython ;; Q|q|quit) exit $REVTAL ;; *) echo "Usage: select one number(1|2|3|4|5|6|7|8|9)" exit $REVTAL ;; esac done
簡單演示:
建立一個名爲flask的虛擬壞境並切換到這個虛擬環境下面
[root@php blog]# pythonbrew venv create flask Creating `flask` environment into /root/.pythonbrew/venvs/Python-2.7.6 Already using interpreter /root/.pythonbrew/pythons/Python-2.7.6/bin/python New python executable in /root/.pythonbrew/venvs/Python-2.7.6/flask/bin/python Installing setuptools.............done. Installing pip...............done. [root@php blog]# pythonbrew venv use flask # Using `flask` environment (found in /root/.pythonbrew/venvs/Python-2.7.6) # To leave an environment, simply run `deactivate` (flask)[root@php blog]# (flask)[root@php blog]# (flask)[root@php blog]#
在這個虛擬環境下安裝版本爲1.6的django
(flask)[root@php ~]# pip install django==1.6 # 安裝版本爲1.6的django Downloading/unpacking django==1.6 Downloading Django-1.6.tar.gz (6.6MB): 6.6MB downloaded Running setup.py egg_info for package django warning: no previously-included files matching '__pycache__' found under directory '*' warning: no previously-included files matching '*.py[co]' found under directory '*' Installing collected packages: django Running setup.py install for django changing mode of build/scripts-2.7/django-admin.py from 644 to 755 warning: no previously-included files matching '__pycache__' found under directory '*' warning: no previously-included files matching '*.py[co]' found under directory '*' changing mode of /root/.pythonbrew/venvs/Python-2.7.6/flask/bin/django-admin.py to 755 Successfully installed django Cleaning up... (flask)[root@php ~]# python -c "import django;print (django.get_version())" # 在咱們建立虛擬環境中打印django的版本 1.6 (flask)[root@php ~]# pythonbrew off [root@php ~]# python -c "import django;print (django.get_version())" # 打印django版本,這裏提示沒有這個模塊 Traceback (most recent call last): File "<string>", line 1, in <module> ImportError: No module named django [root@php ~]#
擼完收工,也就是一個工具而已,沒必要在乎這些細節,能用就好,python多版本管理工具不少的,找一個適合本身的就行,多了反而會被其所左右~