在CentOS系统上安装Python 3.8可以通过以下步骤完成:
首先,打开终端。
下载Python 3.8或3.10.0的源代码:
wget https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tgz
wget https://www.python.org/ftp/python/3.10.0/Python-3.10.0.tgz
解压下载的源码包:
cd到Python-3.8.0.tgz的目录
tar xzvf Python-3.8.0.tgz
安装必要的构建工具和依赖项:
sudo yum groupinstall -y "Development Tools" 这个是安装开发工具包
sudo yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel gcc make 这个是安装依赖
配置安装目录,编译和安装Python 3.8:
cd Python-3.8.0
./configure --prefix=/usr/local/python3.8
./configure --prefix=/usr/local/python3.8/ --with-ssl 【启用安全(可选)】
如果你想要一个包含所有稳定优化(PGO等)的发布版本,请执行(建议不执行,后面有答案): ./configure --enable-optimizations
make
sudo make install
或者:推荐使用(sudo make && make install)
注意: 如果使用make altinstall,而不是make install可以避免覆盖默认的Python版本。
成功:
Looking in links: /tmp/tmpgtcb5cjh
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-19.2.3 setuptools-41.2.0
验证安装:
/usr/local/python3.8/bin/python3.8 --version
或python3.8 --version
以上步骤会在CentOS系统中安装Python 3.8,可以选择保留系统中原有的Python版本。使用python3.8来运行Python 3.8,而python或python2将继续指向系统中的默认Python 2版本。
(可选)创建软链接:
为了方便使用,你可以为python3和pip3命令创建软链接。例如:
sudo ln -s /usr/local/python3.8/bin/python3.8 /usr/bin/python3
sudo ln -s /usr/local/python3.8/bin/pip3.8 /usr/bin/pip3
配置环境变量
vim ~/.bash_profile
#配置python
export PYTHON_HOME=/usr/local/python3.8
export PATH=$PYTHON_HOME/bin:$PATH
生效验证
source ~/.bash_profile
echo $PYTHON_HOME
注:需要安装3.10.0,只更改3.8.0即可
清华源:Simple Indexhttps://pypi.tuna.tsinghua.edu.cn/simple
报错:
make build_all CFLAGS_NODIST=" -fprofile-use -fprofile-correction" LDFLAGS_NODIST=""
make[1]: Entering directory /usr/local/src/Python-3.8.0' ./python -E -S -m sysconfig --generate-posix-vars ;\ if test $? -ne 0 ; then \ echo "generate-posix-vars failed" ; \ rm -f ./pybuilddir.txt ; \ exit 1 ; \ fi Could not import runpy module Traceback (most recent call last): File "/usr/local/src/Python-3.8.0/Lib/runpy.py", line 15, in <module> import importlib.util File "/usr/local/src/Python-3.8.0/Lib/importlib/util.py", line 14, in <module> from contextlib import contextmanager File "/usr/local/src/Python-3.8.0/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/src/Python-3.8.0’
make: *** [profile-opt] Error 2
解决方法:
yum update 更新(可以不操作,视情况而定)
必须操作
1、yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel libffi-devel gcc make 安装依赖
2、下载python https://www.python.org/ftp/python/ 找到自己合适的版本
3、解压python到指定目录并进入解压出来的目录 tar zxf Python-3.8.0.tgz
4、./configure --prefix=/usr/local/python3 注意不要加(前面的建议没听吧) --enable-optimizations
5、make clean 清空缓存 (重要)
6、make && make install
7、添加软链接
ln -s /usr/local/python3/bin/python3.8 /usr/local/bin/python3.8
ln -s /usr/local/python3/bin/pip3.8 /usr/local/bin/pip3.8
8、输入python3.8即可