1.通过按pip install virtualenv
2.安装pyenv(推荐)
安装相关环境包:
#centos、redhat
yum groupinstall "Development tools"
yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel
#readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
#-------------------------------------------------------------------------------
#Ubuntu、debain
apt-get install curl libssl-dev libncurses5-dev python-dev
或完整安装:
sudo apt-get install libc6-dev gcc
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm
#-------------------------------------------------------------------------------
#安装 pyenv
curl https://raw.github.com/yyuu/pyenv-installer/master/bin/pyenv-installer |bash
#或者
git clone git://github.com/yyuu/pyenv.git .pyenv #.pyenv 为虚拟目录
3.创建环境变量:
[root@bogon ~]# cat /etc/profile.d/pyenv.sh
#!/bin/bash
#################
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
[root@bogon ~]#source /etc/profile.d/pyenv.sh
[root@bogon ~]# cat ~/.pip/pip.conf #设置pip国内源(可选);
[global]
index-url = http://pypi.douban.com/simple/
[root@bogon ~]#
apt-get install readline
4.测试
[root@bogon ~]# pyenv install --list #显示可安装的包
[root@bogon ~]# pyenv install 3.4.3 #安装python3.4.3
[root@bogon ~]# pyenv local system #本地切换系统默认的python版本
[root@bogon ~]# python -V #查看系统当前的版本
Python 2.7.5
[root@bogon ~]# pyenv local 3.4.3 #本地切换python3.4.3版本
[root@bogon ~]# pyenv shell 3.4.3 #和上类似
[root@bogon ~]# python -V
Python 3.4.3
[root@bogon ~]#pyenv global system
[root@bogon ~]# pyenv versions #查看系统当前可用的python版本,以及正在使用的版本
system
* 3.4.3 (set by /root/.python-version)