目前Linux 默认自带安装python 2, 后续由于多种原因配置安装了python3 (3.6 or 3.7) 后,发现误操作 python2 不可用,且安装文件已丢失。需要重新安装,在此仅仅记录个人在已安装python高版本情况下,重装低版本过程。过程入错错误,请留言指正!仅为随笔。
1. 官网下载相应的Python 安装包(python2.7 )此处
官网下载路径:
https://www.python.org/downloads/release/python-2716/
Note:
2. 安装
1)将下载好的压缩包,上传到服务器的某个目录下
# cd /usr/local/[root@bogon bin]# cd /usr/local/ [root@bogon local]# ls bin games lib libexec postgresql Python-2.7.16 python3 sbin src etc include lib64 man python27 Python-2.7.16.tgz python3.7 share
2)# 解压
tar -xvzf Python-2.7.16.tgz
3) # 创建安装文件夹
mkdir /usr/local/python27
4)# 编译安装
cd Python-2.7.16
./configure --prefix=/usr/local/python27 --with-threads --enable-shared
make && make altinstall
5) #替换以前的python默认版本(创建新的软连接)
# cd /usr/bin/
# rm -rf python
# ln -s /usr/local/python27/bin/python2.7 ./python
# ls -l # 查询 /usr/bin/ 软连接
6) # 验证 python version
python --version
[root@bogon local]# python --version Python 2.7.5 [root@bogon local]#
7) 执行python 报错
python
[root@bogon lib]# python Could not find platform independent libraries
Could not find platform dependent libraries Consider setting $PYTHONHOME to [: ] ImportError: No module named site [root@bogon lib]# [root@bogon lib]# 解决方法:
1. 查询GCC version
[root@bogon bin]# [root@bogon bin]# gcc --version gcc (GCC) 7.3.0 Copyright (C) 2017 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. [root@bogon bin]#
2. 配置环境变量
export PYTHONHOME=/usr/local/python27
export PYTHONPATH=$PYTHONPATH:/usr/local/python27/lib/python2.7
到此 执行python 没问题了
此时验证GCC:
[root@bogon local]# gcc --version
gcc (GCC) 7.3.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[root@bogon local]# python
Python 2.7.5 (default, Oct 14 2020, 14:45:30)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>