Centos7 配置LAMP环境-Python3

一、环境准备

1.下载Python的安装包,可从官网下载上传到主机,也可以用wget直接下载。

[root@localhost ~]# cd /home/software/
[root@localhost ~]#  wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz

2.解压

[root@localhost software]# tar -zxvf Python-3.7.3.tar.xz

3.安装gcc/gcc-c++
下面这个结果,代表已经安装gcc

[root@localhost ~]# gcc -v
gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)

还有一种可能是,有包但是没安装

[root@localhost ~]# yum list gcc*
Avaliable Packages
gcc.x86_64
gcc-c++.x86_64

如果结果中没有gcc-c++,或者只有”gcc-c++ ”安装包,但是没有安装的情况,说明需要安装,安装代码如下:

[root@localhost ~]# yum -y install gcc-c++

4.安装Python依赖包

 [root@localhost ~]# yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel 

二、安装Python

1.配置编辑参数

[root@localhost ~]# cd /home/software/Python-3.7.3
[root@localhost Python-3.7.3]# ./configure --prefix=/usr/local/bin/python3 --enable-shared --enable-universalsdk

参数讲解:
–enable-shared 保证python会创建共享库(shared library),否则只会创建静态库(static library)
–enable-universalsdk 保证python会被编译成64位

2.编译和安装
[root@localhost Python-3.7.3]# make
[root@localhost Python-3.7.3]# make install

安装完成后,若出现以下两行,说明安装成功;

Installing collected packages: setuptools, pip
Successfully installed pip-19.0.3 setuptools-39.0.1

也可以用“echo $?”查看,结果为0说明安装成功
在这里插入图片描述
3.使用Python3
在这里插入图片描述
可以看到,虽然成功安装了python3.7.3,但是执行起来很繁琐,可以通过创建软链接,将Python指向python3

4.创建python软链接指向python3命令

首先查看python命令的绝对路径,可以看到它是通过软链接执行到python2.7的。
在这里插入图片描述
首先,删除原有的软链接

[root@localhost ~]# rm -rf /usr/bin/python   

创建软链接

[root@localhost ~]# ln -s /usr/local/bin/python3/bin/pytho3 /usr/bin/python

在这里插入图片描述
5.将pip指向python3.7

 [root@localhost ~]# ln-s /usr/local/bin/python3/bin/pip3.7 /usr/bin/pip
 [root@localhost ~]# pip --version
  pip 19.0.3 from /usr/local/bin/python3/lib/python3.7/site-packages/pip (python 3.7)

在这里插入图片描述
6.配置yum命令

修改完python的默认版本后,会存在一个问题,无法执行yum命令

首先,修改yum命令文件,将头文件改为python2.7

 [root@localhost ~]# vim /usr/bin/yum

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

如果修改完之后,运行yum,出现以下错误:

File “/usr/libexec/urlgrabber-ext-down”, line 28
except OSError, e:
^
SyntaxError: invalid syntax

那么需要修改以下两点:
第一点:

[root@localhost ~]# vim /usr/libexec/urlgrabber-ext-down

#!/usr/bin/python修改为#!/usr/bin/python2.7
第二点:

[root@localhost ~]# vim /usr/bin/yum-config-manager

解决办法同上:将#!/usr/bin/python修改为#!/usr/bin/python2.7

猜你喜欢

转载自blog.csdn.net/weixin_43840640/article/details/89492165