关于python2&python3并存使用
 

在目前大多数服务器默认还是2.7的大环境下,如何同时兼容python2与python3 是常常需要解决的问题,以下是我整理的配置方式。

Centos7 依赖环境

yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel python-devel \
 libffi-devel  tk-devel
# py3.7 依赖

python3安装

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.7 /usr/bin/
ln -s /usr/local/bin/python3.7 /usr/bin/python3
# 快捷方式

Macos 依赖环境

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版本路径

brew install openssl
brew unlink openssl
brew link openssl




wget -c https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz # 下载最新稳定版

tar xf Python*xz
cd Python*

Modules/Setup.dist

SSL=/usr/local/Cellar/openssl/1.0.2s
_ssl _ssl.c \
        -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
        -L$(SSL)/lib -lssl -lcrypto
./configure --prefix=/usr/local

make && make  altinstall
# altinstall在安装时会区分已存在的版本

ln -s  /usr/local/bin/python3.7 /usr/local/bin/python3

替换py2

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 ln -s /usr/local/bin/python3.6 /usr/bin/python3

2.anaconda方式安装

anaconda

pip 包管理

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
#官网下载安装

virtualenv虚拟环境

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

sqlite问题处理

https://www.sqlite.org/download.html
# 官网
sqlite3 --version # 当前版本
wget https://www.sqlite.org/2019/sqlite-autoconf-3270200.tar.gz
tar -zxvf sqlite-autoconf-3270200.tar.gz
cd sqlite-autoconf-3270200
./configure --prefix=/usr/local
make && make install
find /usr/ -name sqlite3
rm /usr/bin/sqlite3 -f && ln -s  /usr/local/bin/sqlite3 /usr/bin/

export LD_LIBRARY_PATH="/usr/local/lib"
# 将路径传递给共享库;export语句写入 ~/.bashrc确保开机启动

MySQL-python

yum install python-devel mysql-devel

pip2.7  install -i http://mirrors.aliyun.com/pypi/simple  --trusted-host mirrors.aliyun.com   MySQL-python

2.anaconda方式安装

anaconda

pip 包管理

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
#官网下载安装

virtualenv虚拟环境

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

sqlite问题处理

https://www.sqlite.org/download.html
# 官网
sqlite3 --version # 当前版本
wget https://www.sqlite.org/2019/sqlite-autoconf-3270200.tar.gz
tar -zxvf sqlite-autoconf-3270200.tar.gz
cd sqlite-autoconf-3270200
./configure --prefix=/usr/local
make && make install
find /usr/ -name sqlite3
rm /usr/bin/sqlite3 -f && ln -s  /usr/local/bin/sqlite3 /usr/bin/

export LD_LIBRARY_PATH="/usr/local/lib"
# 将路径传递给共享库;export语句写入 ~/.bashrc确保开机启动

MySQL-python

yum install python-devel mysql-devel

pip2.7  install -i http://mirrors.aliyun.com/pypi/simple  --trusted-host mirrors.aliyun.com   MySQL-python
-->

文章作者: 以谁为师
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源!
              
  目录