一、背景
因为业务量新增,公司给了台新的服务器,因为代码是用python写的,版本是3.10.4,所以需要在服务器上装python3.10.4,结果发现linux自带的python软连接被删了
二、怎么发现软连接被删的
发现/usr/bin下没有python了,一发现的时候还是比较慌的,还有点不相信,就选了个会用到python的命令进行验证,结果就是啪啪打脸:
[root@pghost5 ~]# yum --help
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
到此,心里只冒出了几个字,“完了,芭比Q了。。。”
三、怎么解决呢?
既然已经可以断定是自带的python软连接被删,那怎么办呢?
根据系统自带的python版本,再次安装对应的版本,比如上图中显示机器自带的是python2.7,那就去官网下载python2.7安装:Welcome to Python.org
[root@pghost5 ~]# tar xvf Python-2.7.5.tar
[root@pghost5 ~]# cd Python-2.7.5
[root@pghost5 Python-2.7.5]# ./configure --prefix=/usr/local/Python2.7
[root@pghost5 Python-2.7.5]# make
[root@pghost5 Python-2.7.5]# make install
安装成功之后,重新建立软连接,这一步很重要:
[root@pghost5 ~]# ln -s /usr/local/python2.7/bin/python3 /usr/bin/python
验证:
[root@pghost5 ~]# python --version
Python 2.7.5
[root@pghost5 ~]# python
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00007fc1eb5c2200 (most recent call first):
Aborted (core dumped)
出现错误,虽然版本能看到,但是python无法使用,这个问题,查了很久,从上午查到下午,最后发现,是因为环境变量, 添加了如下的值:
vi /etc/profile
PYTHONHOME=/usr/local/python3.10/lib/python3.10
PYTHONPATH=/usr/local/python3.10/lib/python3.10
将这些都注释掉,source /etc/profile
再次验证:
[root@pghost5 ~]# python
Python 2.7.5 (default, Jun 20 2019, 20:27:34)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
出现>>>,说明可以使用了,成功