django學習之pythonbrew部署開發環境

關於pythonbrew的介紹:https://github.com/utahta/pythonbrew

中文文檔:http://pythonbrew.readthedocs.org/en/latest/



選擇pythonbrew的原因

  • 融合了virtualenv,創建隔離環境更方便快捷

  • 具有pyenv的所擁有的所有功能(個人感覺更強悍)


使用示例

  • 列出可安裝的 python 版本:pythonbrew list --know

  • 安裝某個版本的 python : pythonbrew install 2.7.3

  • 刪除已安裝的某版本的 python : pythonbrew uninstall 2.7.3

  • 列出已安裝的 python 版本(當前使用的版本後會用星號標記): pythonbrew list

  • 使用某個版本的 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
+-------------------------------------------+
|1、Install epel_install		    |
|2、Install base_packages	            |
|3、Install pip_install			    |
|4、Install pythonbrew_install 		    |
|5、Install pythonbrew_env		    |
|6、Install python_install		    |
|7、Install install_ipython		    |
|8、One-Click Setup			    |
|9、[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多版本管理工具很多的,找一個適合自己的就行,多了反而會被其所左右~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章