0%

记录-Centos7安装py环境

Pre:

环境: Centos7 64位


安装py3:

py3版本:3.6.3

1
2
# 准备编译环境
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel -y
1
2
# 安装编译所需依赖
yum install gcc-c++ gcc -y
1
2
# 下载 Python3.6代码包
wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tar.xz
1
2
# 解压
tar xf Python-3.6.3.tar.xz -C /usr/local/src/
1
2
3
4
# 编译安装
cd /usr/local/src/Python-3.6.3/
./configure --prefix=/usr/local/python3
make -j 4 && make install

安装成功后提示

1
2
3
4
# 做软链:
ln -s /usr/local/python3/bin/python3.6 /usr/local/bin/python3
ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
1
2
# 升级pip到最新版本:
pip3 install --upgrade pip

搞定.


同时再安装py3.9:

1
2
# 下载 Python3.9源码包
wget https://www.python.org/ftp/python/3.9.5/Python-3.9.5.tgz
1
2
# 解压
tar xf Python-3.9.5.tgz -C /usr/local/src/
1
2
3
4
# 编译安装
cd /usr/local/src/Python-3.9.5/
./configure --prefix=/usr/local/python3.9
make -j 4 && make install
1
2
3
4
# 做软链:
ln -s /usr/local/python3.9/bin/python3 /usr/local/bin/python3.9
ln -s /usr/local/python3.9/bin/python3 /usr/bin/python3.9
ln -s /usr/local/python3.9/bin/pip3 /usr/bin/pip3.9
1
2
# 升级pip到最新版本:
pip3.9 install --upgrade pip

注意路径,然后不要把原来的另一个python3版本的覆盖了就好。


安装py2:

py2版本:2.7.13

1
2
3
# 准备编译环境
yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel
1
2
3
# 下载&解压
wget https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz
tar -zxvf Python-2.7.13.tgz
1
2
3
4
5
# 安装
cd Python-2.7.13
./configure --prefix=/usr/local
make
make altinstall
1
2
3
4
5
# 备份原有系统默认的py
mv /usr/bin/python /usr/bin/python.bak

# 软链接:
ln -s /usr/local/bin/python2.7 /usr/bin/python

1
2
3
4
5
6
# 安装pip
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py

# 软链接:
ln -s /usr/local/bin/pip2.7 /usr/bin/pip

修改yum配置:

1
2
3
4
5
6
7
# 修改yum文件头部,要指向系统默认的Python

# 查看/usr/bin/目录下所有yum文件头部
ll /usr/bin/yum*  
# 修改/usr/bin/目录下所有yum文件头部,一般就修改/usr/bin/yum这个
vi /usr/bin/yum*  
#!/usr/bin/python 修改成: #!/usr/bin/python2.7  
1
2
3
# 修改urlgrabber-ext-down头部
vi /usr/libexec/urlgrabber-ext-down  
#!/usr/bin/python 修改成 #!/usr/bin/python2.7

包的迁移:

1
2
3
# 导出
pip2 freeze > py2requirements.txt
pip3 freeze > py3requirements.txt
1
2
3
4
# 安装
pip install -r requirements.txt
# 加aliyun代理
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/

注意:pip会顺序从列表上安装下来,遇到某个有问题的包后,会退出.后面的包不会被安装到.

一些无法或很难安装的包,可以尝试通过复制来迁移

1
2
3
4
5
# 找到第三包的安装路径
>>> import sys
>>> print sys.path

['', '/usr/local/lib/python27.zip', '/usr/local/lib/python2.7', '/usr/local/lib/python2.7/plat-linux2', '/usr/local/lib/python2.7/lib-tk', '/usr/local/lib/python2.7/lib-old', '/usr/local/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/site-packages']

pip其他使用场景:

1
2
3
4
5
# pip批量安装跳过无法安装的包继续运行
while read requirement; do sudo pip3 install $requirement; done < requirement.txt
# example:
while read requirement; do sudo pip2 install $requirement -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mir
rors.aliyun.com; done < py2requirements.log

refs: