一.源码安装
1.安装依赖包:
[root@qfedu.com ~]# yum groupinstall "Development Tools"
[root@qfedu.com ~]# yum -y install zlib-devel bzip2-devel openssl-devel sqlite-devel readline-devel libffi-devel
- 1
- 2
2.下载源码包
去python的官网python.org
orient/strip%7CimageView2/2/w/1240)]
点击直接下载或者复制链接地址
[root@qfedu.com ~]# wget https://www.python.org/ftp/python/3.7.6/Python-3.7.6.tar.xz
- 1
3.解压并且安装
[root@qfedu.com ~]# tar -xf Python-3.7.6.tar.xz
[root@qfedu.com ~]# cd Python-3.7.6
- 1
- 2
- 3
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
- 1
- 2
- 3
- 4
- 5
- 6
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
- 1
- 2
- 3
- 4
- 5
5.开始编译安装
[root@qfedu.com ~]# ./configure --enable-shared
[root@qfedu.com ~]# make -j 1 && make install
-j 当前主机的 cpu 核心数,根据自己的核数可以修改
- 1
- 2
- 3
- 4
–enable-shared 指定安装共享库,共享库在使用其他需调用python的软件时会用到,比如使用
mod_wgsi
连接Apache与python时需要。
二.配置环境
依次执行以下命令
[root@qfedu.com ~]# cmd1='export LD_LIBRARY_PATH='
[root@qfedu.com ~]# cmd2='$LD_LIBRARY_PATH:/usr/local/lib'
[root@qfedu.com ~]# file="/etc/profile.d/python3_lib.sh"
[root@qfedu.com ~]# echo "${cmd1}${cmd2}" >$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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
执行以下命令使环境配置生效
[root@qfedu.com ~]# ldconfig
[root@qfedu.com ~]# source /etc/profile
- 1
- 2
三.测试安装
1.测试python3
[root@qfedu.com ~]# python3 -V
Python 3.7.6
[root@qfedu.com ~]#
#显示的含有python3.7.6就没问题
- 1
- 2
- 3
- 4
2.测试pip3
[root@qfedu.com ~]# pip3 -V
pip 20.0.2 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
- 1
- 2
输出的信息中的目录
/usr/local/lib/python3.7/site-packages/
是用于存放 安装的第三方模块的
四.配置使用本地的源安装第三方模块
1.在当前用户的家目录下创建一个隐藏的目录 .pip
2.执行如下命令,方便写入国内的源:
[root@qfedu.com ~]# echo '[global]' >> ~/.pip/pip.conf
[root@qfedu.com ~]# c1="index-url=https://"
[root@qfedu.com ~]# c2="mirrors.aliyun.com/pypi/simple"
[root@qfedu.com ~]# echo "${c1}${c2}" >> ~/.pip/pip.conf
- 1
- 2
- 3
- 4
豆瓣源:
https://pypi.douban.com/simple/
阿里源:https://mirrors.aliyun.com/pypi/simple
3.测试配置正确行
可以安装一个增强版的解释器ipython
用于测试后面也会用的这个模块
[root@qfedu.com ~]# pip3 install ipython
- 1
————python3