关于pythonbrew的介绍:https://github.com/utahta/pythonbrew
选择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)[[email protected] ~]# 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的虚拟坏境并切换到这个虚拟环境下面
[[email protected] 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. [[email protected] 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)[[email protected] blog]# (flask)[[email protected] blog]# (flask)[[email protected] blog]#
- 在这个虚拟环境下安装版本为1.6的django
(flask)[[email protected] ~]# 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)[[email protected] ~]# python -c "import django;print (django.get_version())" # 在我们创建虚拟环境中打印django的版本 1.6 (flask)[[email protected] ~]# pythonbrew off [[email protected] ~]# 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 [[email protected] ~]#
撸完收工,也就是一个工具而已,不必在意这些细节,能用就好,python多版本管理工具很多的,找一个适合自己的就行,多了反而会被其所左右~
时间: 2024-10-16 04:33:45