Centos7 安装Python3
Centos7 安装Python3
一、linux安装Python3
- 1.安装相关依赖
[root@3f9 ~]# yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel gcc
- 2.官网下载Python源码包
[root@3f9 ~]# wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz
- 3.创建安装目录
[root@3f9 ~]# mkdir -p /usr/local/python3
- 4.解压文件
[root@3f9 ~]# tar -zxvf Python-3.6.4.tgz
- 5.进入解压目录
[root@3f9 ~]# cd Python-3.6.4
- 6.配置
[root@3f9 Python-3.6.4]# ./configure --prefix=/usr/local/python3 --enable-shared --enable-optimizations
# --enable-shared启用共享,方便其他依赖python的一些内置库(比如 mysqlclient) 的资源的正常# 安装,在pyinstaller python打包时,也需要开启 --enable-shared
# --enable-optimizations是优化选项(LTO,PGO 等)加上这个 flag 编译后,性能有 10% 左右的优化
- 7.编译安装
[root@3f9 Python-3.6.4]# make && make install
- 8.将python3加入环境变量
[root@3f9 ~]# vim ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
# 将python3的路径加入此文件
PATH=$PATH:$HOME/bin:/usr/local/python3/bin
export PATH
[root@3f9 ~]# source ~/.bash_profile
- 9.创建python3软连接
[root@3f9 ~]# ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
[root@3f9 ~]# python -V
- 10.创建pip3软连接,升级pip3
[root@3f9 ~]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
[root@3f9 ~]# pip3 -V
[root@3f9 ~]# pip3 install --upgrade pip
二、问题
1.运行python3 -V报错
python3: error while loading shared libraries: libpython3.6m.so.1.0: cannot open shared object file: No such file or directory
1. 将python3安装目录的lib目录下的文件libpython3.6m.so.1.0复制到/usr/lib/目录下
[root@3f9 ~]# cp /usr/local/python3/lib/libpython3.6m.so.1.0 /usr/lib/
2.将目录/usr/lib/添加到/etc/ld.so.conf.d/目录下的python3.6.conf文件中
[root@3f9 ~]# echo "/usr/lib" > /etc/ld.so.conf.d/python3.6.conf
3.刷新动态链接库
[root@3f9 ~]# ldconfig