CentOS7安装升级Python3
一、查看默认的Python版本
默认安装的python2
[root@server ~]# python
Python 2.7.5 (default, Oct 14 2020, 14:45:30)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[root@server ~]# python3
bash: python3: 未找到命令...
相似命令是: 'python'
[root@server ~]# pip -V
bash: pip: 未找到命令..
[root@server ~]# pip3 -V
bash: pip3: 未找到命令...
[root@server ~]#.
[root@server ~]#
[root@server ~]# ll -lsh /usr/bin/python*
0 lrwxrwxrwx. 1 root root 7 1月 8 2022 /usr/bin/python -> python2
0 lrwxrwxrwx. 1 root root 9 1月 8 2022 /usr/bin/python2 -> python2.7
8.0K -rwxr-xr-x. 1 root root 7.0K 10月 14 2020 /usr/bin/python2.7
[root@server ~]#
二、yum方式安装的python3
[root@server ~]# yum install python3
[root@server ~]# pip -V
bash: pip: 未找到命令...
[root@server ~]# pip3 -V
pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)
[root@server ~]# python3
Python 3.6.8 (default, Nov 14 2023, 16:29:52)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
查看软连接
[root@server ~]# ll -lsh /usr/bin/python*
0 lrwxrwxrwx. 1 root root 7 1月 8 2022 /usr/bin/python -> python2
0 lrwxrwxrwx. 1 root root 9 1月 8 2022 /usr/bin/python2 -> python2.7
8.0K -rwxr-xr-x. 1 root root 7.0K 10月 14 2020 /usr/bin/python2.7
0 lrwxrwxrwx 1 root root 9 1月 12 15:16 /usr/bin/python3 -> python3.6
12K -rwxr-xr-x 2 root root 12K 11月 15 00:31 /usr/bin/python3.6
12K -rwxr-xr-x 2 root root 12K 11月 15 00:31 /usr/bin/python3.6m
[root@server ~]#
三、编译安装更高版本的python3
[root@server ~]# tar zxvf Python-3.7.0.tgz
[root@server ~]# cd Python-3.7.0/
[root@server Python-3.7.0]# ls
aclocal.m4 configure Grammar Lib Makefile.pre.in Objects PCbuild Python Tools
config.guess configure.ac Include LICENSE Misc Parser Programs README.rst
config.sub Doc install-sh Mac Modules PC pyconfig.h.in setup.py
[root@server Python-3.7.0]# ./configure --prefix=/usr/local/python3
// 执行完会到最后会提示如下信息。
If you want a release build with all stable optimizations active (PGO, etc),
please run ./configure --enable-optimizations
// 安装前先安装一下依赖包,已安装忽略
[root@server Python-3.7.0]# yum install libffi-devel zlib* -y
//3.7版本需要安装libffi-devel包否则报错,Module Not Found Error: No module named '_ctypes'
[root@server Python-3.7.0]# ./configure --prefix=/usr/local/python3 --enable-optimizations
[root@server Python-3.7.0]# make && make install
四、创建软连接
[root@server Python-3.7.0]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln: 无法创建符号链接"/usr/bin/python3": 文件已存在
[root@server Python-3.7.0]#
[root@server ~]# rm -rf /usr/bin/python3
[root@server ~]# ll -lsh /usr/bin/python*
0 lrwxrwxrwx. 1 root root 7 1月 8 2022 /usr/bin/python -> python2
0 lrwxrwxrwx. 1 root root 9 1月 8 2022 /usr/bin/python2 -> python2.7
8.0K -rwxr-xr-x. 1 root root 7.0K 10月 14 2020 /usr/bin/python2.7
12K -rwxr-xr-x 2 root root 12K 11月 15 00:31 /usr/bin/python3.6
12K -rwxr-xr-x 2 root root 12K 11月 15 00:31 /usr/bin/python3.6m
[root@server ~]#
[root@server ~]# python3
Python 3.6.8 (default, Nov 14 2023, 16:29:52)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[root@server ~]#
[root@server Python-3.7.0]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
[root@server Python-3.7.0]#
[root@server ~]# ll -lsh /usr/bin/python*
0 lrwxrwxrwx. 1 root root 7 1月 8 2022 /usr/bin/python -> python2
0 lrwxrwxrwx. 1 root root 9 1月 8 2022 /usr/bin/python2 -> python2.7
8.0K -rwxr-xr-x. 1 root root 7.0K 10月 14 2020 /usr/bin/python2.7
0 lrwxrwxrwx 1 root root 30 1月 12 17:30 /usr/bin/python3 -> /usr/local/python3/bin/python3
12K -rwxr-xr-x 2 root root 12K 11月 15 00:31 /usr/bin/python3.6
12K -rwxr-xr-x 2 root root 12K 11月 15 00:31 /usr/bin/python3.6m
[root@server ~]#
查看新版本
[root@server ~]# python3
Python 3.7.0 (default, Jan 12 2024, 17:25:19)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
五、添加到PATH
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
PATH=$PATH:$HOME/bin:/usr/local/python3/bin
export PATH
//修改生效
source ~/.bash_profile
六、检查python3和pip,升级pip,有可能网速卡,多试几次
[root@server ~]# python3 -V
Python 3.7.0
[root@server ~]#
[root@server Python-3.7.0]# pip3 -V
pip 10.0.1 from /usr/local/python3/lib/python3.7/site-packages/pip (python 3.7)
[root@server ~]#
[root@server Python-3.7.0]# pip3 install --upgrade pip
Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.org', port=443): Read timed out. (read timeout=15)")': /simple/pip/
Collecting pip
Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7fa0937f0ac8>: Failed to establish a newconnection: [Errno 101] 网络不可达')':
/packages/15/aa/3f4c7bcee2057a76562a5b33ecbd199be08cdb4443a02e26bd2c3cf6fc39/pip-23.3.2-py3-none-any.whl
Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7fa0937f05c0>: Failed to establish a newconnection: [Errno 101] 网络不可达')': /packages/15/aa/3f4c7bcee2057a76562a5b33ecbd199be08cdb4443a02e26bd2c3cf6fc39/pip-23.3.2-py3-none-any.whl
Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. (read timeout=15)")': /packages/15/aa/3f4c7bcee2057a76562a5b33ecbd199be08cdb4443a02e26bd2c3cf6fc39/pip-23.3.2-py3-none-any.whl
Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. (read timeout=15)")': /packages/15/aa/3f4c7bcee2057a76562a5b33ecbd199be08cdb4443a02e26bd2c3cf6fc39/pip-23.3.2-py3-none-any.whl
Downloading https://files.pythonhosted.org/packages/15/aa/3f4c7bcee2057a76562a5b33ecbd199be08cdb4443a02e26bd2c3cf6fc39/pip-23.3.2-py3-none-any.whl (2.1MB)
100% |████████████████████████████████| 2.1MB 11kB/s
Installing collected packages: pip
Found existing installation: pip 10.0.1
Uninstalling pip-10.0.1:
Successfully uninstalled pip-10.0.1
Successfully installed pip-23.3.2
[root@server Python-3.7.0]#
[root@server Python-3.7.0]# pip3 -V
pip 23.3.2 from /usr/local/python3/lib/python3.7/site-packages/pip (python 3.7)
[root@server Python-3.7.0]#