白日依山尽,黄河入海流。欲穷千里目,更上一层楼。 -- 唐·王之涣

Centos下pyenv安装python多版本(增强版)

Centos 下 pyenv 安装 python 多版本

在学习和利用python开发的很多情况下,需要多版本的Python并存。此时需要在系统中安装多个Python,但又不能影响系统自带的 Python。pyenv 就是这样一个 Python 版本管理器。

当前情况

1
2
3
4
5
6
7
8
9
## 当天系统
root@pts/2 $ cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)

## 当前 python 情况
root@pts/2 $ python
python python2 python2.7 python2.7-config python2-config python-config
root@pts/2 $ python -V
Python 2.7.5

安装 pyenv

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
## 安装
root@pts/2 $ git clone https://github.com/yyuu/pyenv.git ~/.pyenv
Cloning into '/root/.pyenv'...
remote: Counting objects: 12744, done.
remote: Total 12744 (delta 0), reused 0 (delta 0), pack-reused 12744
Receiving objects: 100% (12744/12744), 2.26 MiB | 453.00 KiB/s, done.
Resolving deltas: 100% (8837/8837), done.

## 配置
root@pts/2 $ vim ~/.bashrc
root@pts/2 $ cat ~/.bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

## add by liuchao at 20160607
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

## 配置生效
### 方式一
重新打开一个session
### 方式二
source ~/.bashrc
### 方式三
exec $SHELL -l

安装多版本

检查pyenv 安装的版本

root@pts/3 $ pyenv versions
* system (set by /root/.pyenv/version)

安装之前需要安装一些依赖包,否则会报如下错误

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
root@pts/2 $ pyenv install 3.4.4
Downloading Python-3.4.4.tgz...
-> https://www.python.org/ftp/python/3.4.4/Python-3.4.4.tgz
Installing Python-3.4.4...
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib?

Please consult to the Wiki page to fix the problem.
https://github.com/yyuu/pyenv/wiki/Common-build-problems


BUILD FAILED (CentOS Linux 7 using python-build 20160509-25-g4c654d7)

Inspect or clean up the working tree at /tmp/python-build.20160607105740.3166
Results logged to /tmp/python-build.20160607105740.3166.log

Last 10 log lines:
(cd /root/.pyenv/versions/3.4.4/share/man/man1; ln -s python3.4.1 python3.1)
if test "xupgrade" != "xno" ; then \
case upgrade in \
upgrade) ensurepip="--upgrade" ;; \
install|*) ensurepip="" ;; \
esac; \
./python -E -m ensurepip \
$ensurepip --root=/ ; \
fi
Ignoring ensurepip failure: pip 7.1.2 requires SSL/TLS

安装相关依赖

1
2
3
4
yum install readline readline-devel readline-static -y
yum install openssl openssl-devel openssl-static -y
yum install sqlite-devel -y
yum install bzip2-devel bzip2-libs -y

安装多版本

  • pyenv 安装原理:
    从官网下载对应的版本压缩包到/tmp/目录,然后在/tmp/目录 执行编译安装,安装到~/.pyenv/versions/下面
  • pyenv 安装很慢的解决办法:
    可以先手动下载对应的版本压缩包,放到~/.pyenv/cache/下面,pyenv会校验md5值和完整性,确认无误的话就不会重新下载直接从这里安装

** >>附件修改 201606008<<**
详细可以参考 pyenv ~/.pyenv/cache 不生效问题

关于上面说的如果用 pyenv install 安装很慢的话,可以下载版本压缩包到 ~/.pyenv/cache/ 目录,这里有个很关键的问题所在,具体如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
添加 -v 参数安装的时候看到下载的文件名字是 'Python-3.4.4.tgz',如果
把这个文件名copy到 ~/.pyenv/cache/ 下面的是不起作用的,还是会继续
重新下载。

查找问题后发现,-v 显示的是下载 'Python-3.4.4.tgz', 但是
在/tmp/python-xxxxxx.xxxx/ 目录下面却显示的是 'Python-3.4.4.tar.gz' 文件。

所以把下载的 'Python-3.4.4.tgz' 改名为 'Python-3.4.4.tar.gz' 后放到
~/.pyenv/cache/ 下面后,然后 pyenv install 3.4.4 -v 就不会重新下载了。

注意:
不能采用把 Python-3.4.4.tgz 解压之后才压缩成 Python-3.4.4.tar.gz 的方式,
因为这样的话会导致源文件的md5值发生变化。而校验失败重新下载

-v 显示完整的安装过程

1
2
3
4
5
root@pts/2 $ pyenv install 3.4.4 `[-v]`
Downloading Python-3.4.4.tgz...
-> https://www.python.org/ftp/python/3.4.4/Python-3.4.4.tgz
Installing Python-3.4.4...
Installed Python-3.4.4 to /root/.pyenv/versions/3.4.4

检查

## 查看目前已经安装的
## system 表示系统安装的
## * 代表当前使用的是那个版本
root@pts/3 $ pyenv versions
* system (set by /root/.pyenv/version)
  3.4.4

更新数据库

pyenv rehash 

设置全局python

root@pts/3 $ python -V
Python 2.7.5

root@pts/3 $ pyenv global 3.4.4 
root@pts/3 $ pyenv versions
  system
* 3.4.4 (set by /root/.pyenv/version)

root@pts/3 $ python -V
Python 3.4.4

设置临时python版本

## 在当前session执行
pyenv local 2.7.11
pyenv shell 2.7.11

## 检查发现是从/tmp来设置的临时
root@pts/3 $ pyenv versions
  system
* 2.7.11 (set by /tmp/python-build.20160607105740.3166/Python-3.4.4/.python-version)
  3.4.4
1
2
3
4
5
6
7
8
## 在另外一个session中验证
root@pts/5 $ python -V
Python 3.4.4
Dev-mysql-mem [~] 2016-06-07 11:44:30
root@pts/5 $ pyenv versions
system
2.7.11
* 3.4.4 (set by /root/.pyenv/version)

Refer to

pyenv命令列表


公众号: DailyJobOps DailyJobOps
作者

Colin

发布于

2016-09-25

许可协议