Linux 配置Pyhon3.8环境
Python 3.8 安装与配置指南
在 CentOS 7 上,默认支持的 Python 3 版本为 3.6。如果您需要使用 PyInstaller 打包应用程序并且需要 Python 3.8,可以通过 SCL(Software Collections)来安装高版本的 Python。
什么是 SCL?
SCL(Software Collections)允许您在同一个操作系统上安装和使用多个版本的软件,而不会影响整个系统的安装包。SCL 旨在满足创建和使用软件集合的需求,支持 Fedora 和 RHEL(包括其衍生版本如 CentOS)。
安装步骤
1. 安装 SCL
首先,您需要安装 SCL 的相关软件包:
yum -y install centos-release-scl-rh centos-release-scl
2. 安装 Python 3.8
接下来,安装 Python 3.8:
yum install rh-python38
3. 设置环境变量
创建环境变量配置文件,以便在系统启动时自动加载 Python 3.8:
cat << EOF >/etc/profile.d/python38.sh
# Enable Python 3.8
source /opt/rh/rh-python38/enable
export X_SCLS="`scl enable rh-python38 'echo \$X_SCLS'`"
EOF
然后,加载环境变量:
source /etc/profile.d/python38.sh
4. 设置 pip 数据源
为了加快 Python 包的下载速度,您可以设置 pip 的数据源:
mkdir -p ~/.pip
cat << EOF > ~/.pip/pip.conf
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = mirrors.aliyun.com
[list]
format = columns
EOF
5. 安装 PyInstaller 和其他依赖
使用 pip 安装所需的库和 PyInstaller:
pip3 install redis
pip3.8 install wheel
pip3.8 install PyInstaller
6. 查看已安装的软件包
您可以使用以下命令查看已安装的软件包:
pip3 list
示例输出:
Package Version
------------------------- -------
altgraph 0.17.3
async-timeout 4.0.2
pip 19.3.1
pyinstaller 5.11.0
pyinstaller-hooks-contrib 2023.3
redis 4.5.5
setuptools 67.8.0
wheel 0.40.0
7. 使用 PyInstaller 打包软件
最后,使用 PyInstaller 打包您的 Python 应用程序:
pyinstaller -w -F -n redis_health_check_rhel7_v1.5 redis_health_check_v1.5.py
注意事项
- 确保在执行上述命令时具有适当的权限(通常需要以 root 用户身份运行)。
- 在安装和配置过程中,确保网络连接正常,以便能够下载所需的软件包。
通过以上步骤,您可以在 CentOS 7 上成功安装和配置 Python 3.8,并使用 PyInstaller 打包您的应用程序。