本文最后更新于 2053 天前,其中的信息可能已经有所发展或是发生改变。
有些软件需要安装python2.7,但centos6默认安装的是2.6版本。但yum依赖2.6,所以不能覆盖安装。需要保留两者
编译安装python2.7
wget https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tgz tar xvf Python-2.7.14.tgz sudo mkdir /usr/local/python2.7 cd Python-2.7.14 ./configure --prefix=/usr/local/python2.7 --enable-shared make sudo make install
验证
cd /usr/local/python2.7/bin ./python
报错:
./python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory
将python2.7的动态链接库加入到系统会读取的配置中
sudo vim /etc/ld.so.conf # 在该配置文件追加一行:/usr/local/python2.7/lib sudo ldconfig
备份原有的python
which python # /usr/bin/python [myhost tmp]$ md5sum /usr/bin/python /usr/bin/python2.6 8505ca7b24f60f8dca8d6d2622ed0563 /usr/bin/python 8505ca7b24f60f8dca8d6d2622ed0563 /usr/bin/python2.6 # 发现已经有python2.6,且与python的md5一致,那就不用备份直接删除了 sudo rm /usr/bin/python
编辑yum的执行程序
sudo vim /usr/bin/yum # 将#!/usr/bin/python 中的python改为python2.6 # 即 #!/usr/bin/python2.6
执行yum验证下,没有报错就ok了
配置python2.7
sudo ln -s /usr/local/python2.7/bin/python /usr/bin/python # 执行python [foam@host tmp]$ python Python 2.7.14 (default, Apr 9 2019, 17:06:25) [GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux2 # ok 正常运行
# 安装pip wget https://bootstrap.pypa.io/get-pip.py python get-pip.py # 软链 sudo ln -s /usr/local/python2.7/bin/pip2.7 /usr/bin/pip # 验证 [foam@host tmp]$ pip -V pip 19.0.3 from /usr/local/python2.7/lib/python2.7/site-packages/pip (python 2.7) # ok 没问题
# 为了能够愉快地在全局运行python安装的软件,将bin加入到环境变量 sudo vim /etc/profile 追加一行:export PATH=/usr/local/python2.7/bin:$PATH