下载Python安装包
cd /usr/local/src
编译时要提前装好gcc编译器和zlib zlib-devel
1、下载文件
wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz
2、解压
tar -zxvf Python-3.6.0.tgz
3、进入解压目录
cd Python-3.6.0
4、编译安装包
./configure --prefix=/usr/local/python make && make install
5、添加环境变量
echo PATH='/usr/local/python/bin/:$PATH' >> /etc/profile
source /etc/profile
6、检查是否成功,执行下面代码
python3.6 Python 3.6.0 (default, Jun 1 2017, 14:01:43)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello word')
hello word
Python安装常见问题:
(1)configure: error: no acceptable C compiler found in $PATH
解决:yum install -y gcc
(2):zipimport.ZipImportError: can’t decompress data
解决:安装zlib zlib-devel
1、修改系统默认的Python路径,因为在终端中输入Python命令时默认是指向Python2.6.6
mv /usr/bin/python /usr/bin/python-2.6.6
2、建立新的软连接,指向Python-3.6.0
ln -s /usr/local/python/bin/python3.6 /usr/bin/python
3、因为yum是依赖python的,所以这里我们修改了默认的python,就要要修改yum,让其运行指向旧的版本:
vi /usr/bin/yum
将第一行中的“#!/usr/bin/python” 修改为“#!/usr/bin/python-2.6.6”,保存即可
4、打开一个新的终端,通过python命令进入python环境,可以看到已经指向了我们新安装的python3.6.0:
[centos65_1@localhost:~]$ python
Python 3.6.0 (default, Jul 30 2016, 19:40:32)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
5、Centos中Python升级为3.X时yum出现except OSError, e: ^ SyntaxError: invalid syntax问题
CentOS 7升级Python到3.6后,需要在/usr/bin/python创建了一个指向Python 3的软连接,然后将/usr/bin/yum的顶部的:
!/usr/bin/python
改成了
!/usr/bin/python-2.7.5
后,运行yum,还是出现了以下错误:
[root@master ~]# yum -y groupinstall “X Window System”
File "/usr/libexec/urlgrabber-ext-down", line 28
except OSError, e:
^
SyntaxError: invalid syntax
解决方案:
(1). vim /usr/libexec/urlgrabber-ext-down
将/usr/bin/python改为/usr/bin/python-2.7.5。
(2). vim /usr/bin/yum-config-manager
解决办法同上: #!/usr/bin/python换成 #!/usr/bin/python-2.7.5