本文共 5587 字,大约阅读时间需要 18 分钟。
选择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):
安装脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | (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的虚拟坏境并切换到这个虚拟环境下面
1 2 3 4 5 6 7 8 9 10 11 12 | [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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | (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多版本管理工具很多的,找一个适合自己的就行,多了反而会被其所左右~
本文转自lovelace521 51CTO博客,原文链接:http://blog.51cto.com/lovelace/1585432,如需转载请自行联系原作者