1.安装相应的编译工具
在root用户下(不要用普通用户,麻烦),全部复制粘贴过去,一次性安装即可.
yum -y groupinstall "Development tools"
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
yum install -y libffi-devel zlib1g-dev
yum install zlib* -y
2.下载安装包
wget https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tar.xz
3.解压
tar -xvfPython-3.8.5.tar.xz
4.创建编译安装目录
mkdir /usr/local/python3
5.安装
cd Python-3.8.5
./configure --prefix=/usr/local/python3 --enable-optimizations --with-ssl #第一个指定安装的路径,不指定的话,安装过程中可能软件所需要的文件复制到其他不同目录,删除软件很不方便,复制软件也不方便. #第二个可以提高python10%-20%代码运行速度. #第三个是为了安装pip需要用到ssl,后面报错会有提到. make && make install
6.创建软链接
ln -s /usr/local/python3/bin/python3 /usr/local/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3
7.验证是否成功
python3 -V
pip3 -V
8.报错处理
错误0:
xxx/usr/local/Python3/bin # ./python3
Fatal Python error: init_import_size: Failed to import the site module
Python runtime state: initialized
ValueError: source code string cannot contain null bytes
Current thread 0x00007f4a1c334740 (most recent call first):
系统中存在多个python版本,默认库路径、python路径不对,找到相应模块路径
PYTHONHOME:指定Python的标准库。
PYTHONPATH:用于扩充Python查找模块文件的路径。
这里是导入site模块时出错,所以应该在PYTHONPATH上增加site模块的路径。
/usr/local/Python3/bin # find /usr/local/Python3/ -name site.py*
/usr/local/Python3/lib/python3.8/site.py
查看确认该模块没有在以下路径:
/usr/local/Python3/bin # echo $PYTHONHOME
/usr/local/Python3/bin # echo $PYTHONPATH
/home/share/Python-2.7/lib:/home/party/spark/python/:/home//share/Python-2.7/lib:/home/share/Python-2.7/lib/site-packages
临时执行:
PYTHONPATH=/usr/local/Python3/lib/python3.8/:$PYTHONPATH ./python3 /data/t.py
将该路劲添加到环境变量:
export PYTHONPATH=/usr/local/Python3/lib/python3.8/:$PYTHONPATH
错误1.
zipimport.ZipImportError: can't decompress data; zlib not available Makefile:1099: recipe for target 'install' failed make: *** [install] Error 1
需要安装依赖
yum -y install zlib1g-dev
错误2.
ModuleNotFoundError: No modulenamed'_ctypes'
需要安装依赖
yum -y install libffi-devel