很多 linux 系统上都会自带 python2 的环境,这里主要记录在 centos7 中安装 python3 。
[root@contos7 local]# yum install gcc-c++ zlib zlib-devel openssl pcre curl-devel
[root@contos7 local]# wget https://www.python.org/ftp/python/3.8.3/Python-3.8.3.tgz
[root@contos7 local]# tar -zxvf Python-3.8.3.tgz -C /usr/local
[root@contos7 local]# cd python3.8.3
#指定编译目录
[root@contos7 local]# ./configure --prefix=/usr/local/python3.8.3 --enable-optimizations
#编译安装
[root@contos7 local]# make && make install
[root@contos7 local]# vi /etc/profile
在打开的编辑器中输入:
export PYTHON_HOME=/usr/local/python3.8.3
export PATH=$PYTHON_HOME/bin:$PATH
配置生效:
[root@contos7 local]# source /etc/profile
[root@contos7 local]# ln -s /usr/local/python3/bin/python3.8 /usr/bin/python3
[root@contos7 local]# ln -s /usr/local/python3/bin/pip3.8 /usr/bin/pip3
python3
pip3
make 编译安装时异常
Could not import runpy module
Traceback (most recent call last):
File "/usr/local/lib/python/3.8.3/Lib/runpy.py", line 15, in
import importlib.util
File "/usr/local/lib/python/3.8.3/Lib/importlib/util.py", line 14, in
from contextlib import contextmanager
File "/usr/local/lib/python/3.8.3/Lib/contextlib.py", line 4, in
import _collections_abc
SystemError: returned NULL without setting an error
generate-posix-vars failed
make[1]: *** [pybuilddir.txt] Error 1
make[1]: Leaving directory `/usr/local/lib/python/3.8.3'
make: *** [profile-opt] Error 2
[guest@kvmvps 3.8.3]$
原因:可能是 gcc 版本太低的问题
解决方法:
1、安装scl源
yum install centos-release-scl
2、安装 gcc、gcc+
yum install devtoolset-7-gcc devtoolset-7-gcc-c++
3、启动:
scl enable devtoolset-7 bash
需要注意的是scl命令启用只是临时的,退出shell或重启就会恢复原系统gcc版本。如果要长期使用gcc 7.3的话:
echo "source /opt/rh/devtoolset-7/enable" >>/etc/profile
4、查看版本:
which gcc
gcc --version
—————————
如有不足请留言指正
相互学习,共同进步
参考地址:
https://liqiang.io/post/install-python3-8-in-centos-973bdb81
https://juejin.im/post/5e1d2810e51d4502044ecab7
https://zhuanlan.zhihu.com/p/33660059
https://www.vpser.net/manage/centos-6-upgrade-gcc.html
https://my.oschina.net/michaelshu/blog/3024970
https://ahelpme.com/linux/centos7/how-to-install-new-gcc-and-development-tools-under-centos-7/