Loading

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

在编译时加入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}
posted @ 2016-09-02 10:34  头痛不头痛  阅读(858)  评论(0编辑  收藏  举报