python的源码安装方法

一.源码安装

1.安装依赖包:

[[email protected] ~]# yum groupinstall "Development Tools"
[[email protected] ~]# yum -y install  zlib-devel bzip2-devel openssl-devel  sqlite-devel readline-devel  libffi-devel

2.下载源码包
去python的官网python.org
image.png
orient/strip%7CimageView2/2/w/1240)]
点击直接下载或者复制链接地址

[[email protected] ~]# wget https://www.python.org/ftp/python/3.7.6/Python-3.7.6.tar.xz

3.解压并且安装

[[email protected] ~]# tar -xf Python-3.7.6.tar.xz

[[email protected] ~]# cd Python-3.7.6

4.修改配置信息
4.1 方式一:直接修改,用vim
修改文件 Python-3.7.6/Modules/Setup.dist, 去掉如下几行的注释 :

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命令提示符下执行以下命令

在[root@qf-cloud-2002 ~]# 下面执行以下命令

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.开始编译安装

[[email protected] ~]# ./configure --enable-shared

[[email protected] ~]# make -j 1 && make install
 -j  当前主机的 cpu 核心数,根据自己的核数可以修改

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

二.配置环境

依次执行以下命令

[[email protected] ~]# cmd1='export LD_LIBRARY_PATH='
[[email protected] ~]# cmd2='$LD_LIBRARY_PATH:/usr/local/lib'
[[email protected] ~]# file="/etc/profile.d/python3_lib.sh"
[[email protected] ~]# echo "${cmd1}${cmd2}" >$file

[[email protected] ~]# path="/usr/local/lib"
[[email protected] ~]# file2="/etc/ld.so.conf.d/python3.conf"
[[email protected] ~]# echo ${path} > $file2

执行以下命令使环境配置生效

[[email protected] ~]# ldconfig
[[email protected] ~]# source /etc/profile

三.测试安装

1.测试python3

[[email protected] ~]#  python3 -V
Python 3.7.6
[[email protected] ~]#
#显示的含有python3.7.6就没问题 

2.测试pip3

[[email protected] ~]# 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
2.执行如下命令,方便写入国内的源:

[[email protected] ~]# echo '[global]' >> ~/.pip/pip.conf
[[email protected] ~]# c1="index-url=https://"
[[email protected] ~]# c2="mirrors.aliyun.com/pypi/simple"
[[email protected] ~]# echo "${c1}${c2}" >> ~/.pip/pip.conf

豆瓣源: https://pypi.douban.com/simple/
阿里源: https://mirrors.aliyun.com/pypi/simple
3.测试配置正确行
可以安装一个增强版的解释器 ipython 用于测试后面也会用的这个模块

[[email protected] ~]# pip3  install ipython

------------python3

猜你喜欢

转载自blog.csdn.net/weixin_49844466/article/details/107922364