如果本机安装了`python2`,尽量不要管他,使用`python3`运行python脚本就好,因为可能有程序依赖目前的`python2`环境,
比如`yum`!!!!!
不要动现有的`python2`环境!
### 安装`python3.7`
#### 安装依赖环境
```bash
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
```
#### 下载Python3
```bash
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
```
#### 安装python3
我个人习惯安装在`/usr/local/python3`(具体安装位置看个人喜好)
创建目录:
```bash
mkdir -p /usr/local/python3
```
解压下载好的`Python-3.x.x.tgz.xz`包
```bash
tar -zxvf Python-3.7.0.tgz
```
#### 进入解压后的目录,编译安装。
```bash
cd Python-3.7.0
./configure --prefix=/usr/local/python3
```
1. make
```bash
cd /usr/local/python3/python3.7.0 && make
```
* 注意:这里执行`make && make install`阿里云会报错,所以分开执行!
2. 报错`ModuleNotFoundError: No module named ‘_ctypes‘`
```bash
yum install libffi-devel -y
```
3. make install
```bash
make install
```
#### 建立`python3`的软链
```bash
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
```
#### 并将`/usr/local/python3/bin`加入PATH
```bash
vim ~/.bash_profile
.bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/usr/local/python3/bin
export PATH
```
按`ESC`,输入`:wq`回车退出。
修改完记得执行行下面的命令,让上一步的修改生效:
```bash
source ~/.bash_profile
```
#### 检查`Python3`及`pip3`是否正常可用:
```bash
python3 -V
# Python 3.7.0
```
```bash
pip3 -V
# pip 10.0.1 from /usr/local/python3/lib/python3.7/site-packages (python 3.7)
```
#### 不行的话在创建一下`pip3`的软链接
```bash
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
```
The end..
原文地址:https://www.cnblogs.com/medivhxu/p/10109856.html