Centos上面默认的Python版本是2.6,本文介绍如何安装3.4版本。
0.下载前准备
需要安装以下库,不然会有问题。
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
先安装sqlite3(因为安装python3.4后,yum需要修改python链接,需要用老python2.6编译)
yum install libsqlite3-dev libsqlite3
1. 下载Python3.4源码
# wget http://mirrors.sohu.com/python/3.4.3/Python-3.4.3.tar.xz
2. 解压缩并安装
# xz -d Python-3.4.3.tar.xz
# tar xf Python-3.4.3.tar -C /usr/local/src/
# cd /usr/local/src/Python-3.4.3/
# ./configure --prefix=/usr/local/python34
# make -j8 && make install
3. 安装的目录
默认情况下,python会安装在
/usr/local/python34
备份旧版本 Python,笔者环境默认版本2.6
mv /usr/bin/python /usr/bin/python2.6
y 回车
新建指向新版本 Python 以及 pip 的软连接
ln -s /usr/local/python34/bin/python3.4 /usr/bin/python
ln -s /usr/local/python34/bin/pip3 /usr/bin/pip
python -V
pip -V
pip升级
ln -s /usr/local/python34/bin/pip3 install --upgrade pip
4. 安装PyMySQL
PyMySQL是python的MySQL库,安装方法如下:
/usr/local/python34/bin/pip3 install PyMySQL
/usr/local/python34/bin/pip3 install --upgrade pip
/usr/local/python34/bin/pip3 install django==1.11.3
/usr/local/python34/bin/pip3 install pillow==4.2.1
/usr/local/python34/bin/pip3 install django-imagekit==4.0.1
/usr/local/python34/bin/pip3 install django_chartit==0.2.9
/usr/local/python34/bin/pip3 install xlwt==1.3.0
/usr/local/python34/bin/pip3 install xlsxwriter==0.9.8
#/usr/local/python34/bin/pip3 install mysql-python
/usr/local/python34/bin/pip3 install pymssql==2.1.3
#/usr/local/python34/bin/pip3 install django-pyodbc-azure
/usr/local/python34/bin/pip3 install django-mssql==1.8
#djangorestframework
/usr/local/python34/bin/pip3 install djangorestframework==3.6.4
/usr/local/python34/bin/pip3 install markdown==2.6.9
/usr/local/python34/bin/pip3 install django-filter==1.0.4
/usr/local/python34/bin/pip3 install requests
#/usr/local/python34/bin/pip3 install urllib3
/usr/local/python34/bin/pip3 install flask
/usr/local/python34/bin/pip3 install isapi_wsgi
/usr/local/python34/bin/pip3 install mod_wsgi
安装新版的ptyon3.4后修改python链接指向到新版本后yum不能使用问题:
[root@name user]# yum
File "/usr/bin/yum", line 30
except KeyboardInterrupt, e:
^
原因:
这是因为yum采用python作为命令解释器,这可以从/usr/bin/yum文件中第一行#!/usr/bin/python发现。而python版本之间兼容性不太好,使得2.X版本与3.0版本之间存在语法不一致问题。而CentOS 6自带的yum采用的是python2.6,当系统将python升级到2.6或3.0后,出现语法解释错误。
解决办法:
很简单,一是升级yum,一是修改yum的解释器为旧版本python2.6(如果你没有采用覆盖升级的话)
升级yum的作法就不详述了;
修改yum的解释器为旧版本python2.6:
vi /usr/bin/yum
将第一行"#!/usr/bin/python" 改为 "#!/usr/bin/python2.6"即可