手动删除python导致的问题和恢复

Creative Commons
本作品采用知识共享署名

处理因删除python3.9导致的pip问题。

情况说明

在ubuntu server 20.02下安装了python 2.7, 3.8.5, 3.9。python默认被设置在3.8.5下, 3.9是之前为了验证问题安装的,现在并没有在使用。
当我安装pyserial包的时候,发现这台server并没有安装pip,于是安装pip:

1
sudo apt-get install python3-pip

再安装pyserial包

1
sudo pip3 install pyserial

此时在python下import serial会提示找不到包,检查发现默认运行的是python 3.8.5,但是pyserial被安装在python3.9下面。由于所有的脚本都是跑在3.8.5下,因此希望pyserial也安装到3.8.5下,卸载没有使用的python3.9:

1
2
3
sudo pip3 uninstall pyserial
sudo apt-get --purge remove python3-pip
sudo apt-get --purge remove python3.9

此时为python 3.8.5安装pip3和pyserial,发现还是被装在/user/lib/python3.9下面,在/usr执行下面命令

1
find -name python3.9*

发现python3.9并没有清除干净

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
./bin/python3.9
./lib/python3.9
./share/man/man1/python3.9.1.gz
./share/binfmts/python3.9
./share/doc/python3.9-doc
./share/doc/python3.9
./share/doc/python3.9/html/python3.9.devhelp.gz
./share/doc/python3.9-minimal
./share/doc-base/python3.9-lib
./share/doc-base/python3.9-inst
./share/doc-base/python3.9-api
./share/doc-base/python3.9-tut
./share/doc-base/python3.9-new
./share/doc-base/python3.9-ext
./share/doc-base/python3.9-dist
./share/doc-base/python3.9-ref
./share/lintian/overrides/python3.9-doc
./share/lintian/overrides/python3.9-minimal
./share/devhelp/books/python3.9
./share/info/python3.9.info.gz
./share/info/python3.9
./include/python3.9
./include/x86_64-linux-gnu/python3.9
./include/x86_64-linux-gnu/python3.9d
./include/python3.9d

于是手动删除

1
find -name python3.9* | xargs sudo rm -rf

然后噩梦开始,此时再安装pip3,会提示已经安装

1
2
3
4
5
6
/usr$ sudo apt-get install python3-pip
Reading package lists... Done
Building dependency tree
Reading state information... Done
python3-pip is already the newest version (20.0.2-5ubuntu1.5).
0 upgraded, 0 newly installed, 0 to remove and 57 not upgraded.

但执行pip,又会说找不到

1
2
/usr$ sudo pip3 install pyserial
sudo: unable to execute /usr/bin/pip3: No such file or directory

如果再次卸载pip3, 提示错误

1
2
3
4
5
6
7
8
9
10
11
12
Removing python3-pip (20.0.2-5ubuntu1.5) ...
/var/lib/dpkg/info/python3-pip.prerm: 6: py3clean: not found
dpkg: error processing package python3-pip (--remove):
installed python3-pip package pre-removal script subprocess returned error exit status 127
dpkg: too many errors, stopping
/var/lib/dpkg/info/python3-pip.postinst: 6: py3compile: not found
dpkg: error while cleaning up:
installed python3-pip package post-installation script subprocess returned error exit status 127
Errors were encountered while processing:
python3-pip
Processing was halted because there were too many errors.
E: Sub-process /usr/bin/dpkg returned an error code (1)

解决方法

1
2
3
4
5
6
7
8
9
sudo apt-get update
sudo apt-get upgrade
sudo apt --fix-broken install
dpkg --configure -a
apt-get download python3-minimal
sudo dpkg -i *python3*.deb
sudo apt-get -f install
sudo apt-get install python3-pip
sudo pip3 install pyserial