- 查看CentOS7中预装的python2.7
[root@localhost ~]# python
Python 2.7.5 (default, Aug 4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
[root@localhost ~]# which python
/usr/bin/python
[root@localhost ~]# cd /usr/bin/
[root@localhost bin]# ls python*
python python2 python2.7
[root@localhost bin]# ls -al python* # 列出所有python开头文件的详细信息
lrwxrwxrwx. 1 root root 7 Aug 19 10:42 python -> python2
lrwxrwxrwx. 1 root root 9 Aug 19 10:42 python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 Aug 4 2017 python2.7
可以看出python指向python2,python2指向python2.7,系统中自带python2.7
- 安装python3
总的来说有五步:
1.下载依赖库
2.wget下载python源码包
3.tar命令解压
4.配置生成Makefile文件
5.编译安装
[root@localhost ~]# yum groupinstall "Development tools" -y
[root@localhost ~]# 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 libffi-devel zlib1g-dev zlib*
[root@localhost ~]# wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tar.xz
[root@localhost ~]# tar xJvf Python-3.7.2.tar.xz
[root@localhost local]# mkdir /usr/local/python3
[root@localhost ~]# cd Python-3.7.2
[root@localhost Python-3.7.2]# ./configure --prefix=/usr/local/python3/ --enable-optimizations # 指定安装路径,启用优化可以提升python代码运行速度
[root@localhost Python-3.7.2]# make && make install # 编译、安装
[root@localhost Python-3.7.2]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3 # 创建软链接,为了能使用python3和pip3命令
[root@localhost Python-3.7.2]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
[root@localhost ~]# python3 -V # 查看是否装好
[root@localhost ~]# pip3 -V
注:在这里为了命令行支持输入python3就能打开python3软件,给python3添加了软链接到/usr/bin/目录下。除此之外,还可以采用添加环境变量的方法
# 临时修改环境变量
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]# export PATH=$PATH:/usr/local/python3/bin/python3
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/python3/bin/python3
# 永久修改
[root@localhost ~]# vim .bashrc # 编辑.bashrc文件,末尾加上添加环境变量语句,保存退出后source文件
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
export PATH=$PATH:/usr/local/python3/bin/python3
- 效果
python3和pip3都装好了
[root@localhost ~]# pip3 -V
pip 18.1 from /usr/local/python3/lib/python3.7/site-packages/pip (python 3.7)
[root@localhost ~]# python3 -V
Python 3.7.2
[root@localhost ~]# python3
Python 3.7.2 (default, Oct 17 2020, 11:46:09)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
测试能否用pip3装第三方库
[root@localhost ~]# pip3 install pandas
[root@localhost ~]# pip3 install pandas
Collecting pandas
Downloading https://files.pythonhosted.org/packages/25/47/22fc373440e144e2111363adaa07abb09ec1f03fbc071b6d9fc0bbf65f68/pandas-1.1.3-cp37-cp37m-manylinux1_x86_64.whl (9.5MB)
100% |████████████████████████████████| 9.5MB 224kB/s
Collecting numpy>=1.15.4 (from pandas)
....
Successfully installed numpy-1.19.2 pandas-1.1.3 python-dateutil-2.8.1 pytz-2020.1 six-1.15.0
搞定!
参考:centos7中安装python3
附:看到有的博客说配置python3时要添加ssl模块否则pip3无法使用,但我这里配置时没加ssl模块,也没报错。可能是这个版本的centos自带的openSSL支持python3的缘故。点这里有说明