分享知识 传递快乐
很多 linux 系统上都会自带 python2 的环境,这里主要记录在 centos7 中安装 python3 。
安装依赖
[root@contos7 local]# yum install gcc-c++ zlib zlib-devel openssl pcre curl-devel
下载 python3 的 linux 运行环境。
[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 <module>
import importlib.util
File "/usr/local/lib/python/3.8.3/Lib/importlib/util.py", line 14, in <module>
from contextlib import contextmanager
File "/usr/local/lib/python/3.8.3/Lib/contextlib.py", line 4, in <module>
import _collections_abc
SystemError: <built-in function compile> 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://zhuanlan.zhihu.com/p/33660059
https://www.vpser.net/manage/centos-6-upgrade-gcc.html
https://ahelpme.com/linux/centos7/how-to-install-new-gcc-and-development-tools-under-centos-7/
标签:gcc,安装,contos7,Centos7,3.8,usr,root,local,Python3 From: https://blog.51cto.com/u_15856116/5813282