Linux CentOS7下安装python3

在CentOS7下,默认安装的就是python2.7,我现在来教大家如何安装python3:

1、首先安装python3.6可能使用的依赖

# yum -y install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel zlib-devel

2、下载python源码包,地址为https://www.python.org/downloads/source/,然后通过xftp上传上去,或者直接使用wget命令,wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz下载到本地

# tar -zxvf Python-3.6..tgz
# cd Python-3.6./

3、把python3.6安装到/usr/local目录下

# ./configure --prefix=/usr/local/
# make
# make altinstall

4、更改/usr/bin/python链接

# cd /usr/bin
# mv python python.backup
# ln -s /usr/local/bin/python3. /usr/bin/python
# ln -s /usr/local/bin/python3. /usr/bin/python3
# rm -rf /usr/bin/python2
# ln -s /usr/bin/python2. /usr/bin/python2

5、更改yum脚本的python依赖

# cd /usr/bin
# ll yum*

有以下文件:

-rwxr-xr-x  root root    Jan   : yum
-rwxr-xr-x root root Jan : yum-builddep
-rwxr-xr-x root root Jan : yum-config-manager
-rwxr-xr-x root root Jan : yum-debug-dump
-rwxr-xr-x root root Jan : yum-debug-restore
-rwxr-xr-x root root Jan : yumdownloader
-rwxr-xr-x root root Jan : yum-groups-manager

修改以上文件头:#!/usr/bin/python 改为 #!/usr/bin/python2

可以安装一个tree测试一下

# yum -y install tree

如果出现以下报错:File “/usr/libexec/urlgrabber-ext-down”, line 28

vim /usr/libexec/urlgrabber-ext-down  

将/usr/bin/python改为/usr/bin/python2即可

这样,yum安装软件的时候就不会报错了,而且默认安装的就是python3了,输入python时,就会进入python3界面,如果想进入python2,那么就输入python2好了。

上一篇:如何编译MongoDB?


下一篇:2018.10.18 NOIP训练 01矩阵(组合数学)