依赖环境
# 依赖libuuid-devel而不是uuid-devel(如果安装了uuid-devel,则需要卸载)
yum -y install gcc gcc-c++ openssl-devel readline-devel bzip2-devel \
sqlite-devel libuuid-devel
# 源码安装openssl
wget https://www.openssl.org/source/openssl-1.0.2s.tar.gz
tar xzf openssl-1.0.2s.tar.gz && cd openssl-1.0.2s
# 添加-fPIC参数,python的hashlib、ssl内置模块不能用动态链接库编译(ssl编译错误日志有打印解决方法)
./config --prefix=/usr/local/openssl -fPIC
make && make install
# 安装libffi(centos6系统自带版本3.0.5太低,编译_ctypes不通过)
# 系统自带libffi不能卸载,很多软件包依赖
wget -O libffi-3.3.tar.gz https://github.com/libffi/libffi/releases/download/v3.3/libffi-3.3.tar.gz
tar xzf libffi-3.3.tar.gz && cd libffi-3.3
./configure --prefix=/usr/local/libffi && make && make install
# 添加动态链接库配置
cat > /etc/ld.so.conf.d/ffi.conf << EOF
/usr/local/libffi/lib64
EOF
# 加载新增动态链接库配置
ldconfig -v | grep libffi
# /usr/local/libffi/lib64:
# libffi.so.7 -> libffi.so.7.1.0 这个必须在第一位,否则编译_ctypes不通过
# libffi.so.5 -> libffi.so.5.0.6
# _uuid异常
# https://conight.com/zh/posts/fix-python-compile-uuid-error/
# 根据需求是否安装gdbm(GNU dbm)
# wget ftp://ftp.gnu.org/gnu/gdbm/gdbm-1.18.1.tar.gz
# tar xzf gdbm-1.18.1.tar.gz && cd gdbm-1.18.1
# ./configure --prefix=/usr/local/gdbm && make && make install
# # export LDFLAGS="-L/usr/local/libffi/lib -L/usr/local/gdbm/lib"
# # 添加动态链接库,import的时候会依赖
# cat > /etc/ld.so.conf.d/gdbm.conf << EOF
# /usr/local/gdbm/lib
# EOF
# # 加载新增动态链接库配置
# ldconfig -v | grep gdbm
安装
# 下载解压
wget https://www.python.org/ftp/python/3.7.7/Python-3.7.7.tgz
tar xzf Python-3.7.7.tgz && cd Python-3.7.7
# 编译安装
export PKG_CONFIG_PATH="/usr/local/libffi/lib/pkgconfig"
export CPPFLAGS="-I/usr/local/gdbm/include"
export LDFLAGS="-L/usr/local/libffi/lib64 -L/usr/local/gdbm/lib"
./configure --prefix=/usr/local/python3 --with-openssl=/usr/local/openssl
make
# 注意查看 Failed to build these modules:
# 测试命令: import ctypes
# _ctypes 和 _ssl 容易失败
# _dbm _lzma _tkinter 这几个可以忽略
make install
# 软连接方便使用
ln -s /usr/local/python3/bin/pip3.7 /usr/bin/
ln -s /usr/local/python3/bin/python3.7 /usr/bin/
# MySQL-python依赖(不建议使用这个模块了,使用pymysql)
# cd /usr/local/python3/lib/python3.7/ && ln -s configparser.py ConfigParser.py
# pip3.7 install MySQL-python
PyMySQL
# 建议安装PyMySQL,这个是目前的主流方案
# MySQL-python在python3已经不支持了
pip3.7 install PyMySQL
安装ipython
创建虚拟环境
# python3.7 -m venv 虚拟环境目录
python3.7 -m venv venv
# 激活
source venv/bin/activate
# 退出
deactivate
修改源
# windows是pip.ini,虚拟环境在对应的虚拟目录即可
# 查看pip配置文件
# pip -v config list
# C:\Users\zaza\pip\pip.ini
mkdir $HOME/.pip
cat > $HOME/.pip/pip.conf << EOF
[global]
index-url=https://mirrors.aliyun.com/pypi/simple/
[list]
format=columns
[install]
trusted-host = mirrors.aliyun.com
EOF