python3部署

python3快速部署脚本

#!/bin/bash
####若手动下载Python3,安装前请将源码包和脚本放一起,并将Python_file的值改成你所下载的文件名
##http://10.9.12.250/2003/python/Python-3.7.6.tar.xz   https://www.python.org/ftp/python/3.7.6/Python-3.7.6.tar.xz
export flag
export Python_file
url=https://www.python.org/ftp/python/3.7.6/Python-3.7.6.tar.xz
Python_file=Python-3.7.6.tar.xz
install_env() {
echo "安装环境中,loading..."
yum -y install gcc gcc-c++ make zlib-devel bzip2-devel openssl-devel  sqlite-devel readline-devel  libffi-devel wget &>/dev/null
[ $? -eq 0 ] &&echo "环境安装完成。"||echo "环境安装失败。"
[ -f $Python_file ]||wget $url  &>/dev/null
[ -f $Python_file ]&&flag=1||flag=0
}
install_file() {
echo "配置编译文件中,loading.."
tar -xf $Python_file
cd  ./Python-*
sed -ri 's/^#readline/readline/' Modules/Setup.dist 
sed -ri 's/^#(SSL=)/\1/' Modules/Setup.dist 
sed -ri 's/^#(_ssl)/\1/' Modules/Setup.dist  
sed -ri 's/^#([\t]*-DUSE)/\1/' Modules/Setup.dist  
sed -ri 's/^#([\t]*-L\$\(SSL\))/\1/' Modules/Setup.dist
echo "配置编译文件完成。"
}
install_make() {
echo "编译中,loading.."
./configure --enable-shared &>/dev/null
make &>/dev/null && make install &>/dev/null
file="/etc/profile.d/python3_lib.sh"
echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib' >$file
path="/usr/local/lib/"
file2="/etc/ld.so.conf.d/python3.conf"
echo $path > $file2
echo "编译完成。"
sleep 2
}
install_env
[ $flag -ne 0 ]&&install_file
[ $flag -ne 0 ]&&install_make&&echo "安装完成,请输入'ldconfig' and 'source /etc/profile' "

一、源码安装

1. 安装依赖软件包

[root@qfedu.com ~]# yum -y install gcc gcc-c++ make zlib-devel bzip2-devel openssl-devel  sqlite-devel readline-devel  libffi-devel wget

2. 下载源码包

image.png

可以直接点击下载,也可以右键 选择复制链接地址。

[root@qfedu.com ~]# wget https://www.python.org/ftp/python/3.7.6/Python-3.7.6.tar.xz

3. 解压安装

[root@qfedu.com ~]# tar -xf Python-3.7.6.tar.xz

[root@qfedu.com ~]# cd Python-3.7.6

4. 修改配置信息

可以选择如下两种方式之一

4.1 方式一:直接使用 vi 修改

修改文件 Python-3.7.6/Modules/Setup.dist, 去掉如下几行的注释 :

如果是 3.8.x 文件是 Setup

readline readline.c -lreadline -ltermcap

SSL=/usr/local/ssl
_ssl _ssl.c \
        -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
        -L$(SSL)/lib -lssl -lcrypto
4.2 方式二:在 shell 命令提示符下执行如下命令:
sed -ri 's/^#readline/readline/' Modules/Setup.dist
sed -ri 's/^#(SSL=)/\1/' Modules/Setup.dist
sed -ri 's/^#(_ssl)/\1/' Modules/Setup.dist 
sed -ri 's/^#([\t]*-DUSE)/\1/' Modules/Setup.dist 
sed -ri 's/^#([\t]*-L\$\(SSL\))/\1/' Modules/Setup.dist

5. 开始编译安装

[root@qfedu.com Python-3.7.6]# ./configure --enable-shared

[root@qfedu.com Python-3.7.6]# make -j 2 && make install
# -j  当前主机的 cpu 核心数

–enable-shared 指定安装共享库,共享库在使用其他需调用python的软件时会用到,比如使用mod_wgsi 连接Apache与python时需要。

二、 配置环境

执行如下命令

[root@qfedu.com ~]# file="/etc/profile.d/python3_lib.sh"
[root@qfedu.com ~]# echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib' >$file

[root@qfedu.com ~]# path="/usr/local/lib/"
[root@qfedu.com ~]# file2="/etc/ld.so.conf.d/python3.conf"
[root@qfedu.com ~]# echo $path > $file2

接下来,执行如下命令使配置好的环境信息生效

[root@qfedu.com ~]# ldconfig
[root@qfedu.com ~]# source /etc/profile

三、 测试安装

1. 测试 python3

[root@qfedu.com ~]#  python3 -V
Python 3.7.6
[root@qfedu.com ~]# 

假如上面显示的含有 Python3.7.6 就没问题了

2. 测试 pip3

[root@qfedu.com ~]# pip3 -V
pip 20.0.2 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)

输出的信息中的目录
/usr/local/lib/python3.7/site-packages/
是用于存放 安装的第三方模块的



四、 配置使用本地的源安装第三方模块

  1. 在当前用户的家目录下创建一个隐藏的目录 .pip
[root@qfedu.com ~]# mkdir ~/.pip
  1. 执行如下命令,以便写入国内的源:
[root@qfedu.com ~]# echo '[global]' >> ~/.pip/pip.conf
[root@qfedu.com ~]# c1="index-url=https://mirrors.aliyun.com/pypi/simple"
[root@qfedu.com ~]# echo "${c1}" >> ~/.pip/pip.conf

豆瓣源: https://pypi.douban.com/simple/
阿里源: https://mirrors.aliyun.com/pypi/simple

  1. 测试配置正确行

可以安装一个增强版的解释器 ipython 用于测试后面也会用的这个模块

[root@qfedu.com ~]# pip3  install ipython
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值