python pyenv 多版本管理(自动安装脚本)

来源:互联网 发布:nginx 读 编辑:程序博客网 时间:2024/05/17 17:42

Summary

比较了多个类似的工具,最后决定选择pyenv, virtualenv过于原始,还有一些不再维护。


Install

下面是在suse上安装的流程:

1) 首先从https://github.com/yyuu/pyenv/archive/master.zip下载原始文件。

2) 在HOME下面建立一个文件夹,名称是 .pyenv

3)    将zip文件解压到 .pyenv中

4) 在 .bashrc中添加如下

export PYENV_ROOT="$HOME/.pyenv"export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

5) source ~/.bashrc

至此整个pyenv都安装完成。

Usage

pyenv install -l 

列举所有的可用的python版本

pyenv install 2.7.5

安装某个版本的python

pyenv versions

列出所有已经安装的版本

pyenv versions
  system
* 2.7.5 (set by /home/jwang/.python-version)

pyenv local 2.7.5

设置所要使用的某一个python版本,当然也可以用global那就是全局的作用了


Issue

bzip2没有找到的bug, 到下面的地址下载一个make然后install

http://www.bzip.org/downloads.html


Advanced

带参数的安装,下面是一个安装动态版本的例子:

env CONFIGURE_OPTS="--enable-shared" pyenv install -v 2.7.5


自动安装脚本

些了一个自动安装脚本

      1 #! /usr/bin/env python      2 import subprocess, os      3 from os.path import expanduser      4       5 # parameters      6 config_file = ".bashrc"      7 -     8 def script(cmd):|     9     subprocess.call(cmd,  shell=True)     10 home = expanduser("~")     11      12 # install pyenv     13 script("unzip ./pyenv-master.zip")     14 env_path = home+"/"+".pyenv"     15 if not os.path.exists(env_path):     16     os.makedirs(env_path)     17 script("cp -rf ./pyenv-master/* ~/.pyenv")     18      19 config_file = home+"/"+config_file     20 print config_file     21 if os.path.exists(config_file) is False:     22     print "Error: can't find config file "+ config_file     23     exit()     24 with open(config_file, 'a') as file:     25     file.write('export PYENV_ROOT="$HOME/.pyenv"\n')     26     file.write('export PATH="$PYENV_ROOT/bin:$PATH"\n')     27     file.write('eval "$(pyenv init -)"\n')     31 os.environ["PYENV_ROOT"] = home+"/"+".pyenv"     32 os.environ["PATH"]  +=  ":"+os.environ["PYENV_ROOT"]+"/bin"     33 script("pyenv init")     34 print os.environ["PATH"]     35 script("source "+config_file)     36 script("pyenv install 2.7.5")     37 script("pyenv rehash")     38 script("pyenv global 2.7.5")     39 script("rm -rf ./pyenv-master")


主要参考:

https://github.com/yyuu/pyenv#understanding-path