centos7安装 python3.6,且保留2.7版本

时间:2023-03-08 20:00:49

CENTOS7安装PYTHON3.6

1. 安装python3.6可能使用的依赖
# yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel

2. 下载python3.6编译安装
新安装的最新centos7最小化安装没有安装wget,所以要安装一下
# yum install wget
# wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz
# tar xzf Python-3.6.0.tgz -C
# cd /Python-3.6.0
把Python3.6安装到 /usr/local 目录,使用make altinstall,如果使用make install,在系统中将会有两个不同版本的Python在/usr/bin/目录中。这将会导致很多问题
# ./configure --prefix=/usr/local
如果遇到:configure: error: no acceptable C compiler found in $PATH
解决方法:# yum install gcc
# make
# make altinstall

3. 更改/usr/bin/python链接
# cd /usr/bin
# mv python python.backup
# ln -s /usr/local/bin/python3.6 /usr/bin/python
# ln -s /usr/local/bin/python3.6 /usr/bin/python3

4. 更改yum脚本的python依赖
# cd /usr/bin
# ls yum*
# vim /usr/bin/yum
# vim /usr/libexec/urlgrabber-ext-down
更改以上文件头为
#!/usr/bin/python 改为 #!/usr/bin/python2

5. python3.6就完成了,测试
[root@T1 bin]# python
Python 3.6.0 (default, Jan 8 2019, 00:23:27)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
[root@T1 bin]# python2.7
Python 2.7.5 (default, Apr 11 2018, 07:36:10)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
能够同时使用python3.6和python2.7