CentOS7 Python 3.7.0 编译安装

2022-12-28 15:33:15 浏览数 (1)

Python的开发环境安装和配置非常的简单,如果是Linux系统则会默认集成安装了Python环境,Python的可执行文件被放在了/usr/local/bin目录下,库函数被安装在了/usr/local/python目录中,接下来我们将使用源码的方式来编译安装一下Python解释器.

1.首先安装gcc编译器,和Python的相关依赖包.

代码语言:javascript复制
[root@localhost ~]# yum -y install gcc zlib zlib-devel openssl openssl-devel libffi-devel

Package gcc-4.8.5-36.el7.x86_64 already installed and latest version
Package zlib-1.2.7-18.el7.x86_64 already installed and latest version
Package zlib-devel-1.2.7-18.el7.x86_64 already installed and latest version
Package 1:openssl-1.0.2k-16.el7.x86_64 already installed and latest version
Package 1:openssl-devel-1.0.2k-16.el7.x86_64 already installed and latest version
Package libffi-devel-3.0.13-18.el7.x86_64 already installed and latest version
Nothing to do

2.这里我们需要编译并安装Python解释器.

代码语言:javascript复制
[root@localhost ~]# yum install -y readline
[root@localhost ~]# wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz
[root@localhost ~]# tar xvzf Python-3.7.0.tgz -C /usr/src/python3.7
[root@localhost ~]# cd /usr/src/Python-3.7.0/
[root@localhost ~]# ./configure --prefix=/usr/local/python3.7
[root@localhost ~]# make && make altinstall

3.将Python头文件拷贝到标准目录,避免直接使用Python找不到所需的头文件.

代码语言:javascript复制
[root@localhost ~]# cd /usr/local/include/python3.7/
[root@localhost ~]# cp -a ./* /usr/local/include/

4.接着我们备份一下旧版本的Python,并创建符号链接链接到新版本的Python上面.

代码语言:javascript复制
[root@localhost ~]# cd /usr/bin/
[root@localhost bin]# mv python python.old
[root@localhost bin]# ln -s /usr/local/bin/python3.7 /usr/local/bin/python
[root@localhost bin]# rm -rf /usr/bin/python
[root@localhost bin]# cp /usr/local/bin/python3.7 /usr/bin/python

5.由于yum是用Python开发的,这里为了避免冲突要改掉他的配置.

代码语言:javascript复制
[root@localhost ~]# vim /usr/bin/yum
#!/usr/bin/python2.7       ←此处将python改成python2.7

[root@localhost ~]# vim /usr/libexec/urlgrabber-ext-down
#!/usr/bin/python2.7       ←此处将python改成python2.7

6.最后测试python新版本是否生效了.

代码语言:javascript复制
[root@localhost ~]# python
Python 3.7.0 (default, Apr 17 2018, 11:03:21) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

0 人点赞