py3compile/py3clean命令缺失导致无法安装/卸载Python及相关软件包,dpkg报错
问题描述
我尝试在环境中安装Python3(和Python2),但持续遇到以下问题:
执行sudo apt-get install python3时得到:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
python3 is already the newest version (3.10.6-1~22.04).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
2 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] y
Setting up python3-minimal (3.10.6-1~22.04) ...
/var/lib/dpkg/info/python3-minimal.postinst: 5: py3compile: not found
dpkg: error processing package python3-minimal (--configure):
installed python3-minimal package post-installation script subprocess returned error exit status 127
Setting up language-selector-gnome (0.219) ...
/var/lib/dpkg/info/language-selector-gnome.postinst: 6: py3compile: not found
dpkg: error processing package language-selector-gnome (--configure):
installed language-selector-gnome package post-installation script subprocess returned error exit status 127
Errors were encountered while processing:
python3-minimal
language-selector-gnome
E: Sub-process /usr/bin/dpkg returned an error code (1)
尝试卸载Python3时(sudo apt remove python3),又出现:
The following NEW packages will be installed:
libqt5gui5-gles libqt5quick5-gles
0 upgraded, 2 newly installed, 217 to remove and 0 not upgraded.
3 not fully installed or removed.
Need to get 0 B/5,427 kB of archives.
After this operation, 276 MB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 261970 files and directories currently installed.)
Removing language-selector-gnome (0.219) ...
/var/lib/dpkg/info/language-selector-gnome.prerm: 6: py3clean: not found
dpkg: error processing package language-selector-gnome (--remove):
installed language-selector-gnome package pre-removal script subprocess returned error exit status 127
/var/lib/dpkg/info/language-selector-gnome.postinst: 6: py3compile: not found
dpkg: error while cleaning up:
installed language-selector-gnome package post-installation script subprocess returned error exit status 127
Removing python3-commandnotfound (22.04.0) ...
/var/lib/dpkg/info/python3-commandnotfound.prerm: 6: py3clean: not found
dpkg: error processing package python3-commandnotfound (--remove):
installed python3-commandnotfound package pre-removal script subprocess returned error exit status 127
dpkg: too many errors, stopping
Errors were encountered while processing:
language-selector-gnome
python3-commandnotfound
Processing was halted because there were too many errors.
E: Sub-process /usr/bin/dpkg returned an error code (1)
尝试安装python3.10-minimal(sudo apt-get install python3.10-minimal)依然报错:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
python3.10-minimal is already the newest version (3.10.6-1~22.04.2).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
3 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] y
Setting up python3-commandnotfound (22.04.0) ...
/var/lib/dpkg/info/python3-commandnotfound.postinst: 6: py3compile: not found
dpkg: error processing package python3-commandnotfound (--configure):
installed python3-commandnotfound package post-installation script subprocess returned error exit status 127
Setting up python3-minimal (3.10.6-1~22.04) ...
/var/lib/dpkg/info/python3-minimal.postinst: 5: py3compile: not found
dpkg: error processing package python3-minimal (--configure):
installed python3-minimal package post-installation script subprocess returned error exit status 127
Setting up language-selector-gnome (0.219) ...
/var/lib/dpkg/info/language-selector-gnome.postinst: 6: py3compile: not found
dpkg: error processing package language-selector-gnome (--configure):
installed language-selector-gnome package post-installation script subprocess returned error exit status 127
Errors were encountered while processing:
python3-commandnotfound
python3-minimal
language-selector-gnome
E: Sub-process /usr/bin/dpkg returned an error code (1)
问题分析
你遇到的是典型的死循环问题:py3compile和py3clean这两个关键工具属于python3-minimal包,但由于之前的Python安装/更新流程被中断,导致这两个工具没有被正确部署到系统中;而所有依赖这些工具的包(包括python3-minimal自身)在安装、卸载或配置时,都会因为找不到工具而失败,进而卡住整个dpkg流程。
解决步骤
下面是适配Ubuntu 22.04环境的修复方法:
下载对应版本的
python3-minimaldeb包
从错误信息可知当前目标版本是3.10.6-1~22.04,执行命令下载该包:sudo apt download python3-minimal=3.10.6-1~22.04如果版本有变动,可通过
apt-cache show python3-minimal | grep Version查看最新匹配版本,替换命令中的版本部分。解压deb包到临时目录
创建临时目录并解压下载好的deb包:mkdir -p /tmp/python3-minimal && dpkg-deb -x python3-minimal_3.10.6-1~22.04_*.deb /tmp/python3-minimal复制缺失工具到系统路径并赋予权限
将解压出的py3compile和py3clean复制到系统默认可执行路径,并添加执行权限:sudo cp /tmp/python3-minimal/usr/bin/py3* /usr/bin/ sudo chmod +x /usr/bin/py3compile /usr/bin/py3clean重新配置所有未完成的包
缺失工具就位后,让dpkg完成所有未结束的配置流程:sudo dpkg --configure -a清理临时文件
确认问题解决后,删除临时目录和下载的deb包:rm -rf /tmp/python3-minimal python3-minimal_3.10.6-1~22.04_*.deb
验证修复
完成上述步骤后,你可以重新尝试安装或卸载Python相关包,比如执行sudo apt-get install python3或sudo apt-get install python2,应该不会再出现py3compile/py3clean找不到的错误了。
备注:内容来源于stack exchange,提问作者KaliTheGreat

