查看系统版本
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
一、安装依赖、下载源码
1、首先安装依赖包
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
2、下载python源码包
官方下载到本地然后上传到服务器或者wget下载
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz
3、解压
tar -zxvf Python-3.7.0.tgz -C /usr/local/
4、进入解压后的目录
cd /usr/local/Python-3.7.0
./configure
make && make install
执行完make&&make install之后,如果出现以下这种报错: “ModuleNotFound:No module named ‘_ctypes’”
这里需要执行:
yum install libffi-devel -y
执行完继续 make && make install
5、软连接
Linux下的软链接其实就相当于Windows下的快捷方式
因为服务器默认的 python/pip 版本是 2,所以建立软连接,将其改为安装版本
- 软连接用法:ln -s 源文件路径 目标文件路径
- 如果目标路径已被使用,就删除对应软连接,重新建立:rm -rf /usr/bin/python
ln -s /usr/local/Python-3.7.0/bin/python3 /usr/bin/python
ln -s /usr/local/Python-3.7.0/bin/pip3 /usr/bin/pip
二、配置yum
yum是依赖python2.7的,你把python改成了3.7了,自然不好使了。但是不用担心,python2.7还在你的系统里。只要修改一下yum里的相关依赖即可
[root@localhost ~]# vim /usr/libexec/urlgrabber-ext-down
#! /usr/bin/python2.7
# A very simple external downloader
# Copyright 2011-2012 Zdenek Pavlas
[root@localhost ~]# vim /usr/bin/yum
#!/usr/bin/python2.7
import sys
try:
三、验证:
[root@localhost ~]# python -V
Python 3.7.0
[root@localhost ~]# pip -V
pip 21.1.3 from /usr/local/Python-3.7.0/lib/python3.7/site-packages/pip (python 3.7)
[root@localhost ~]# python
Python 3.7.0 (default, Jul 17 2021, 16:22:45)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>