环境说明:
centos7 云服务器,阿里云。
安装python版本:Python-3.6.5
安装编译依赖包,不可省略
yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
yum install openssl-devel -y
yum install zlib-devel -y
下载python3版本包
https://www.python.org/ftp/python/ # 这个是Python安装包的官方文件路径
# 好接下来我们下载安装包 源码安装(PS:Liux通用,wget命令需要安装自行搜索如何安装)
wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
# 解压
tar -zxvf Python-3.6.5.tgz
# 进入Python3.6.5解压完后的目录下
cd Python-3.6.5
# yum/ap-get install sqlite-devel # 这个是解决后面No module named ‘_sqlite3’问题
# 编译安装 熟练的话可以使用&& 把以下三条命令连用
./configure prefix=/usr/local/python3 # 如果编译安装失败,可能是要安装C环境: yum/apt-get install -y gcc
make
sudo make install
创建python软链接,替换python3命令指向python
ln -s /usr/local/python3/bin/python3 /usr/bin/python
如果报错ln: creating symbolic link `/usr/bin/python': File exists
请执行
mv /usr/bin/python /usr/bin/python_old
然后在执行上一步
验证环境是否安装成功
[root@instance-7notzmbo ~]# python
Python 3.6.5 (default, Apr 20 2019, 19:34:00)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
创建pip软链接
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip
验证
[root@instance-7notzmbo ~]# pip --version
pip 9.0.3 from /usr/local/python3/lib/python3.6/site-packages (python 3.6)