CentOS7下Python3环境部署

一、系统版本

[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
[root@localhost ~]# getenforce
Disabled
[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Wed 2020-05-06 19:10:18 CST; 11s ago
     Docs: man:firewalld(1)
  Process: 695 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 695 (code=exited, status=0/SUCCESS)

May 06 16:27:42 localhost.localdomain systemd[1]: Starting firewalld - dynami...
May 06 16:27:46 localhost.localdomain systemd[1]: Started firewalld - dynamic...
May 06 19:10:18 localhost.localdomain systemd[1]: Stopping firewalld - dynami...
May 06 19:10:18 localhost.localdomain systemd[1]: Stopped firewalld - dynamic...
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]#

二、配置更新yum源

[root@localhost ~]#cd /etc/yum.repos.d
[root@localhost yum.repos.d]#mount /dev/cdrom /mnt/cdrom
[root@localhost ~]#yum install wget* -y
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# wget http://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost yum.repos.d]# wget http://mirrors.aliyun.com/repo/epel-7.repo
[root@localhost yum.repos.d]# ls
b  Centos-7.repo  CentOS-Media.repo.bak  epel-7.repo
[root@localhost yum.repos.d]#

三、安装依赖

[root@localhost yum.repos.d]# yum -y install zlib-devel bzip2-devel openssl-devel openssl-static ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel lzma gcc
[root@localhost yum.repos.d]# yum -y groupinstall "Development tools"

四、下载Python3.6.2安装包

[root@localhost yum.repos.d]# cd /usr/local/src/
[root@localhost src]# wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xz

五、解压&移动

[root@localhost src]# tar xvf Python-3.6.2.tar.xz
[root@localhost src]# mv Python-3.6.2 /usr/local/python-3.6
[root@localhost src]# cd /usr/local/python-3.6/
[root@localhost python-3.6]# 

六、编译&&安装

[root@localhost python-3.6]# pwd
/usr/local/python-3.6
[root@localhost python-3.6]# ./configure --prefix=/usr/local/sbin/python-3.6

[root@localhost python-3.6]# make && make install

1.安装完成后,出现这几行说明Python已成功安装完成了:

Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-9.0.1 setuptools-28.8.0
[root@localhost python-3.6]#

2.不过现在的Python3还未配置好环境变量,所以快捷方式启动的Python还是旧的,要想让启动更加便捷,还需要进一步的后续配置

[root@localhost python-3.6]# /usr/local/sbin/python-3.6/bin/python3.6
Python 3.6.2 (default, May  6 2020, 19:58:02) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[root@localhost python-3.6]# python -V
Python 2.7.5
[root@localhost python-3.6]# 

七、配置环境变量,绝对路径的修改

1.查看当前的python命令链接指向

[root@localhost python-3.6]# 
[root@localhost python-3.6]# which python
/usr/bin/python
[root@localhost python-3.6]# ll /usr/bin/ | grep python
lrwxrwxrwx.   1 root root          7 May  5 18:20 python -> python2
lrwxrwxrwx.   1 root root          9 May  5 18:20 python2 -> python2.7
-rwxr-xr-x.   1 root root       7216 Aug  7  2019 python2.7
[root@localhost python-3.6]#

2.将python命令通过软链接指向到python3命令

[root@localhost python-3.6]# rm -rf /usr/bin/python
[root@localhost python-3.6]# ln -sv /usr/local/sbin/python-3.6/bin/python3
python3            python3.6-config   python3.6m-config  
python3.6          python3.6m         python3-config     
[root@localhost python-3.6]# ln -sv /usr/local/sbin/python-3.6/bin/python3 /usr/bin/python
‘/usr/bin/python’ -> ‘/usr/local/sbin/python-3.6/bin/python3’
[root@localhost python-3.6]# ll /usr/bin/ | grep python
lrwxrwxrwx    1 root root         38 May  6 20:12 python -> /usr/local/sbin/python-3.6/bin/python3
lrwxrwxrwx.   1 root root          9 May  5 18:20 python2 -> python2.7
-rwxr-xr-x.   1 root root       7216 Aug  7  2019 python2.7
[root@localhost python-3.6]# python -V
Python 3.6.2
[root@localhost python-3.6]# 

3.修改完python默认版本之后,会存在不能执行yum命令的问题,需要做一些修改,如下:

①将/usr/bin/yum的顶部的:

!/usr/bin/python  改成  !/usr/bin/python2.7 

②将/usr/libexec/urlgrabber-ext-down的顶部的:

/usr/bin/python  改为   /usr/bin/python2.7

③将/usr/bin/yum-config-manager的顶部的【注:如果找不到此文件可直接跳过这步】

#!/usr/bin/python 改为 #!/usr/bin/python2.7

4.最后将pip指向到python3.6

[root@localhost python-3.6]# ln -s /usr/local/sbin/python-3.6/bin/pip3 /usr/bin/pip
[root@localhost python-3.6]# pip --version
pip 9.0.1 from /usr/local/sbin/python-3.6/lib/python3.6/site-packages (python 3.6)
[root@localhost python-3.6]# 

完!


原文地址:https://www.cnblogs.com/ashjo009/p/12832550.html