Python3的安装
1. 安装依赖库(因为没有这些依赖库可能在源代码构件安装时因为缺失底层依赖库而失败)#
yum -y groupinstall 'Development Tools'
yum -y install wget gcc zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
2. 下载Python源代码并解压缩到指定目录#
可以在官方网站查询要安装的版本
这里以3.11.3为例
VERSION=3.11.3
wget https://www.python.org/ftp/python/${VERSION}/Python-${VERSION}.tgz -P /usr/local/src/
tar -xvf /usr/local/src/Python-${VERSION}.tgz -C /usr/local
3. 编译安装#
加上--enable-optimizations编译时间会变的很长,操作系统库很旧得话还可能安装报错
cd /usr/local/Python-${VERSION}
./configure --prefix=/usr/local/python${VERSION} --enable-optimizations
make && make install
rm -rf /usr/local/Python-${VERSION}
4. 建立链接#
alternatives --install /usr/bin/python3 python3 /usr/local/python${VERSION}/bin/python3 1
alternatives --install /usr/bin/pip3 pip3 /usr/local/python${VERSION}/bin/pip3 1
5. 修改pip仓库源#
国内源
清华:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学:http://pypi.hustunique.com/
山东理工大学:http://pypi.sdutlinux.org/
豆瓣:https://pypi.douban.com/simple/
临时更改方法 在使用pip时加上-i参数即可
pip install -i http://mirrors.aliyun.com/pypi/simple/ flask
永久修改的方法
mkdir ~/.pip/
touch ~/.pip/pip.conf
cat > ~/.pip/pip.conf <<EOF
[global]
index-url = https://mirrors.aliyun.com/pypi/simple
[install]
trusted-host = mirrors.aliyun.com
EOF
# 修改pip/setup.py的源
cat > ~/.pydistutils.cfg <<EOF
[easy_install]
index_url = http://mirrors.aliyun.com/pypi/simple
EOF
阿里云提供的脚本修改:
wget http://image-offline.oss-cn-hangzhou.aliyuncs.com/fix/fix_pypi.sh
bash fix_pypi.sh "mirrors.aliyun.com/pypi/simple/"
6. 出现问题#
- 使用pip3安装软件时出现: pip is configured with locations that require TLS/SSL
在编译时加入OpenSSL的目录
./configure --prefix=/usr/local/python-${VERSION} --with-openssl=/usr/local/openssl
7. 一键安装#
read -p "请输入要安装的版本(https://www.python.org/downloads/): " -e version
VERSION=${version:-3.11.5}
yum -y groupinstall 'Development Tools'
yum -y install wget gcc zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
wget https://www.python.org/ftp/python/${VERSION}/Python-${VERSION}.tgz -P /usr/local/src/
tar -xvf /usr/local/src/Python-${VERSION}.tgz -C /usr/local
cd /usr/local/Python-${VERSION}
./configure --prefix=/usr/local/python${VERSION}
make && make install
ln -s /usr/local/python${VERSION} /usr/local/python3
cat <<EOF >> /etc/profile
export PATH=/usr/local/python3/bin:$PATH
EOF
mkdir ~/.pip/
touch ~/.pip/pip.conf
cat > ~/.pip/pip.conf <<EOF
[global]
index-url = https://mirrors.aliyun.com/pypi/simple
[install]
trusted-host = mirrors.aliyun.com
EOF
# 修改pip/setup.py的源
cat > ~/.pydistutils.cfg <<EOF
[easy_install]
index_url = http://mirrors.aliyun.com/pypi/simple
EOF
rm -rf /usr/local/Python-${VERSION}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了