在目前大多数服务器默认还是2.7的大环境下,如何同时兼容python2与python3 是常常需要解决的问题,以下是我整理的配置方式。
yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel python-devel -y
#依赖环境
yum install libffi-devel -y
#py3.7 依赖
brew install zlib
ln -s /usr/local/Cellar/zlib/1.2.11/include/* /usr/local/include/
ln -s /usr/local/Cellar/zlib/1.2.11/* /usr/local/lib/
# 根据实际zlib版本路径
wget -c https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz # 下载最新稳定版
tar xf Python*xz
cd Python*
./configure --prefix=/usr/local
make && make altinstall
#altinstall在安装时会区分已存在的版本
ln -s /usr/local/bin/python3.6 /usr/bin/
ln -s /usr/local/bin/python3.6 /usr/bin/python3
#快捷方式
ls /usr/bin/yum* |xargs sed -i 's/python/python2/'
sed -i 's/python/python2/' /usr/libexec/urlgrabber-ext-down
#如果把默认python覆盖了,yum文件要替换成python旧版本
~~cd /usr/bin~~ ~~mv python python.backup~~ ~~ln -s /usr/local/bin/python3.6 /usr/bin/python~~ l~~n -s /usr/local/bin/python3.6 /usr/bin/python3~~
pip --version
pip3.6 --version
#查看现有pip版本
tar zxf pip-9.0.1.tar.gz
sudo python2.7 setup.py install
sudo python3.6 setup.py install
#官网下载安装
pip install virtualenv #python3 安装虚拟环境
pip2 install virtualenv #python2 安装虚拟环境
python2.7 -m virtualenv env2.7
source env2.7/bin/activate
#创建和进入虚拟环境
python3.6 -m virtualenv env3.6
source env3.6/bin/activate