記錄一次CentOS環境升級Python2.6到Python2.7並安裝最新版pip

背景介紹html

一次實驗中須要安裝python-etcd包。安裝這個包時要求的python和pip版本比目前系統的版本高。python

 

系統是centos6.6    64位sql

1
2
3
4
5
6
7
[root@minion01 ~] # uname -r
2.6.32-504.el6.x86_64
[root@minion01 ~] # uname -m
x86_64
[root@minion01 ~] # cat /etc/redhat-release
CentOS release 6.6 (Final)
[root@minion01 ~] #

 

使用pip安裝python-ectd包時提示命令找不到,須要先安裝python-pip,pip是python的包管理工具bootstrap

1
2
3
4
5
6
[root@minion01 ~] # pip install python-etcd
- bash : pip:  command  not found
[root@minion01 ~] # yum install python-pip -y
Installed:
   python-pip.noarch 0:7.1.0-1.el6     此處省略安裝過程    
[root@minion01 ~] #      

 

繼續安裝python-ectd,提示pip版本過低,須要先升級pip。vim

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@minion01 ~] # pip install python-etcd
/usr/lib/python2 .6 /site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_ .py:90: InsecurePlatformWarning: A  true  SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For  more  information, see https: //urllib3 .readthedocs.org /en/latest/security .html #insecureplatformwarning.
   InsecurePlatformWarning
You are using pip version 7.1.0, however version 9.0.1 is available.
You should consider upgrading via the  'pip install --upgrade pip'  command .
Collecting python-etcd
/usr/lib/python2 .6 /site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_ .py:90: InsecurePlatformWarning: A  true  SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For  more  information, see https: //urllib3 .readthedocs.org /en/latest/security .html #insecureplatformwarning.
   InsecurePlatformWarning
   Downloading python-etcd-0.4.3. tar .gz
     Complete output from  command  python setup.py egg_info:
     Traceback (most recent call last):
       File  "<string>" , line 20,  in  <module>
       File  "/tmp/pip-build-dByULi/python-etcd/setup.py" , line 12,  in  <module>
         if  sys.version_info.major == 2:
     AttributeError:  'tuple'  object has no attribute  'major'
     
     ----------------------------------------
Command  "python setup.py egg_info"  failed with error code 1  in  /tmp/pip-build-dByULi/python-etcd

 

按照上面的提示先升級pipcentos

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@minion01 ~] # pip install --upgrade pip
You are using pip version 7.1.0, however version 9.0.1 is available.
You should consider upgrading via the  'pip install --upgrade pip'  command .
/usr/lib/python2 .6 /site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_ .py:90: InsecurePlatformWarning: A  true  SSLContext object is not available. <br>This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For  more  information, see https: //urllib3 .readthedocs.org /en/latest/security .html #insecureplatformwarning.
   InsecurePlatformWarning
Collecting pip
   Downloading pip-9.0.1-py2.py3-none-any.whl (1.3MB)
     100% |████████████████████████████████| 1.3MB 46kB /s
Installing collected packages: pip
   Found existing installation: pip 7.1.0
     Uninstalling pip-7.1.0:
       Successfully uninstalled pip-7.1.0
Successfully installed pip-9.0.1

 

繼續執行命令,提示python版本過低bash

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@minion01 ~] # pip install python-etcd
DEPRECATION: Python 2.6 is no longer supported by the Python core team, please upgrade your Python. A future version of pip will drop support  for  Python 2.6
Collecting python-etcd
   Using cached python-etcd-0.4.3. tar .gz
     Complete output from  command  python setup.py egg_info:
     Traceback (most recent call last):
       File  "<string>" , line 1,  in  <module>
       File  "/tmp/pip-build-okuCUU/python-etcd/setup.py" , line 12,  in  <module>
         if  sys.version_info.major == 2:
     AttributeError:  'tuple'  object has no attribute  'major'
     
     ----------------------------------------
Command  "python setup.py egg_info"  failed with error code 1  in  /tmp/pip-build-okuCUU/python-etcd/

 

查看當前系統默認的python版本app

1
2
[root@minion01 ~] # python -V
Python 2.6.6

 

 

下載並解壓python2.7.10python2.7

新建一個tools目錄用於存放下載的tar包,下載並解壓,命令以下ide

1
2
3
4
5
6
[root@minion01 ~] # mkdir /tools -p
[root@minion01 ~] # cd /tools
[root@minion01 tools] # wget http://www.python.org/ftp/python/2.7.10/Python-2.7.10.tar.xz
[root@minion01 tools] # unxz Python-2.7.10.tar.xz
[root@minion01 tools] # tar -xf Python-2.7.10.tar
[root@minion01 tools] # cd Python-2.7.10

 

配置編譯參數

1
[root@minion01 Python-2.7.10] # ./configure --enable-shared --enable-loadable-sqlite-extensions --with-zlib

 

修改一個文件

1
2
[root@minion01 Python-2.7.10] # vim ./Modules/Setup
找到 #zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz去掉註釋並保存,而後進行編譯和安裝

 

編譯和安裝

1
[root@minion01 Python-2.7.10] # make && make install

 

查看安裝後的結果,備份原先的可執行文件,給新的文件作軟連接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@minion01 Python-2.7.10] # ll /usr/local/bin/
total 48
-rwxr-xr-x 1 root root   101 Dec 19 13:29 2to3
-rwxr-xr-x 1 root root    99 Dec 19 13:29 idle
-rwxr-xr-x 1 root root    84 Dec 19 13:29 pydoc
lrwxrwxrwx 1 root root     7 Dec 19 13:29 python -> python2
lrwxrwxrwx 1 root root     9 Dec 19 13:29 python2 -> python2.7
-rwxr-xr-x 1 root root  9776 Dec 19 13:29 python2.7
-rwxr-xr-x 1 root root  1687 Dec 19 13:29 python2.7-config
lrwxrwxrwx 1 root root    16 Dec 19 13:29 python2-config -> python2.7-config
lrwxrwxrwx 1 root root    14 Dec 19 13:29 python-config -> python2-config
-rwxr-xr-x 1 root root 18547 Dec 19 13:29 smtpd.py
[root@minion01 Python-2.7.10] # mv /usr/bin/python  /usr/bin/python_bak
[root@minion01 Python-2.7.10] # ln -s /usr/local/bin/python2.7 /usr/bin/python

 

再次查看版本,遇到以下報錯,修改配置文件

1
2
3
4
5
6
7
8
9
10
[root@minion01 Python-2.7.10] # python -V
python: error  while  loading shared libraries: libpython2.7.so.1.0: cannot  open  shared object  file : No such  file  or directory
<br>編輯配置文件,添加新的一行內容 /usr/local/lib ,保存退出
[root@minion01 Python-2.7.10] # vim /etc/ld.so.conf<br><br>查看添加後的結果,在第一行
[root@minion01 Python-2.7.10] # cat /etc/ld.so.conf
/usr/local/lib
include ld.so.conf.d/*.conf<br><br>執行ldconfig加載配置
[root@minion01 Python-2.7.10] # ldconfig<br><br>再次執行查看版本命令,正確顯示
[root@minion01 Python-2.7.10] #  python -V
Python 2.7.10

 

再次執行安裝python-ectd,有報錯。pip有問題

1
2
3
4
5
6
[root@minion01 tools] # pip install python-etcd
Traceback (most recent call last):
   File  "/usr/bin/pip" , line 7,  in  <module>
     from pip  import  main
ImportError: No module named pip
[root@minion01 tools] #

 

爲了解決上面問題,手動安裝pip,依次執行下面命令

1
2
3
4
5
[root@minion01 Python-2.7.10] # cd ..
[root@minion01 tools] # wget https://bootstrap.pypa.io/get-pip.py
[root@minion01 tools] # python get-pip.py
[root@minion01 tools] # mv /usr/bin/pip  /usr/bin/pip_bak
[root@minion01 tools] # ln -s /usr/local/bin/pip2.7 /usr/bin/pip

 

再次安裝python-ectd,成功執行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
再次安裝python-etcd,成功了
[root@minion01 tools] # pip install python-etcd
Collecting python-etcd
   Using cached python-etcd-0.4.3. tar .gz
Collecting urllib3>=1.7.1 (from python-etcd)
   Downloading urllib3-1.19.1-py2.py3-none-any.whl (104kB)
     100% |████████████████████████████████| 112kB 65kB /s
Collecting dnspython (from python-etcd)
   Downloading dnspython-1.15.0-py2.py3-none-any.whl (177kB)
     100% |████████████████████████████████| 184kB 68kB /s
Building wheels  for  collected packages: python-etcd
   Running setup.py bdist_wheel  for  python-etcd ...  done
   Stored  in  directory:  /root/ .cache /pip/wheels/a4/72/72/513173535d5ec25c11dbb53ce7c53c92746bff62b362a402a7
Successfully built python-etcd
Installing collected packages: urllib3, dnspython, python-etcd
Successfully installed dnspython-1.15.0 python-etcd-0.4.3 urllib3-1.19.1
[root@minion01 tools] #
相關文章
相關標籤/搜索