一切都很顺利,直到今天我决定升级 Ubuntu16.04.3到16.04.4
在安装下载的更新的最后阶段,python-minimal
软件包安装失败,并出现以下错误:
Setting up python-minimal (2.7.12-1~16.04) ...
Traceback (most recent call last):
File "/usr/local/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/local/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/usr/local/lib/python2.7/compileall.py", line 16, in <module>
import struct
File "/usr/local/lib/python2.7/struct.py", line 1, in <module>
from _struct import *
ImportError: No module named _struct
dpkg: error processing package python-minimal (--configure):
subprocess installed post-installation script returned error exit status 1
Errors were encountered while processing:
python-minimal
E: Sub-process /usr/bin/dpkg returned an error code (1)
我以为重新安装 python 和适当的软件包会有所帮助。但不幸的是,我最终卸载了所有依赖 python 的 python 软件包和软件,如 gimp、mplayer、kde、kicad、pycharm 等。
现在,每次我尝试安装 python 时都会出现以下错误:
$ sudo apt-get install python
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python-minimal python2.7 python2.7-minimal
Suggested packages:
python-doc python-tk python2.7-doc
The following NEW packages will be installed:
libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python python-minimal python2.7 python2.7-minimal
0 upgraded, 7 newly installed, 0 to remove and 21 not upgraded.
Need to get 1,628 kB/3,877 kB of archives.
After this operation, 16.6 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libpython2.7-minimal amd64 2.7.12-1ubuntu0~16.04.3 [340 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 python2.7-minimal amd64 2.7.12-1ubuntu0~16.04.3 [1,261 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 python-minimal amd64 2.7.12-1~16.04 [28.1 kB]
Fetched 1,628 kB in 6s (248 kB/s)
Selecting previously unselected package libpython2.7-minimal:amd64.
(Reading database ... 250996 files and directories currently installed.)
Preparing to unpack .../libpython2.7-minimal_2.7.12-1ubuntu0~16.04.3_amd64.deb ...
Unpacking libpython2.7-minimal:amd64 (2.7.12-1ubuntu0~16.04.3) ...
Selecting previously unselected package python2.7-minimal.
Preparing to unpack .../python2.7-minimal_2.7.12-1ubuntu0~16.04.3_amd64.deb ...
Unpacking python2.7-minimal (2.7.12-1ubuntu0~16.04.3) ...
Selecting previously unselected package python-minimal.
Preparing to unpack .../python-minimal_2.7.12-1~16.04_amd64.deb ...
Unpacking python-minimal (2.7.12-1~16.04) ...
Selecting previously unselected package libpython2.7-stdlib:amd64.
Preparing to unpack .../libpython2.7-stdlib_2.7.12-1ubuntu0~16.04.3_amd64.deb ...
Unpacking libpython2.7-stdlib:amd64 (2.7.12-1ubuntu0~16.04.3) ...
Selecting previously unselected package python2.7.
Preparing to unpack .../python2.7_2.7.12-1ubuntu0~16.04.3_amd64.deb ...
Unpacking python2.7 (2.7.12-1ubuntu0~16.04.3) ...
Selecting previously unselected package libpython-stdlib:amd64.
Preparing to unpack .../libpython-stdlib_2.7.12-1~16.04_amd64.deb ...
Unpacking libpython-stdlib:amd64 (2.7.12-1~16.04) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for bamfdaemon (0.5.3~bzr0+16.04.20180209-0ubuntu1) ...
Rebuilding /usr/share/applications/bamf-2.index...
Processing triggers for desktop-file-utils (0.22-1ubuntu5.1) ...
Processing triggers for gnome-menus (3.13.3-6ubuntu3.1) ...
Processing triggers for mime-support (3.59ubuntu1) ...
Setting up libpython2.7-minimal:amd64 (2.7.12-1ubuntu0~16.04.3) ...
Setting up python2.7-minimal (2.7.12-1ubuntu0~16.04.3) ...
Linking and byte-compiling packages for runtime python2.7...
Setting up python-minimal (2.7.12-1~16.04) ...
Traceback (most recent call last):
File "/usr/local/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/local/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/usr/local/lib/python2.7/compileall.py", line 16, in <module>
import struct
File "/usr/local/lib/python2.7/struct.py", line 1, in <module>
from _struct import *
ImportError: No module named _struct
dpkg: error processing package python-minimal (--configure):
subprocess installed post-installation script returned error exit status 1
Errors were encountered while processing:
python-minimal
E: Sub-process /usr/bin/dpkg returned an error code (1)
我也无法安装任何需要 python 的软件。
升级失败后我的当前 ubuntu 版本:
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.4 LTS
Release: 16.04
Codename: xenial
有人遇到过类似的问题并能解决吗?任何帮助都将不胜感激。
答案1
经过几个小时的努力和数百次安装/卸载 Python 包,我似乎能够安装 Python 包,包括python-minimal
。让我分享我的成功故事。
一开始,我甚至无法卸载 python-minimal。每次安装/卸载时都会出现我在问题中提到的错误。即使尝试过
sudo apt-get -f install
也没有任何收获。不知怎么的,我发现它只能与其他几个软件包一起卸载。以下命令,我用来完成删除python-minimal
损坏和残留的软件包:sudo apt-get autoremove libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python-minimal python2.7 python2.7-minimal
这将删除所有 python 包和依赖 python 的软件。我知道这很极端,但相信我,我别无选择。
sudo apt-get remove --purge $(sudo dpkg -l | egrep "^(rc|iU)" | awk '{print $2}' | tr '\n' ' ')
如果有的话,去除其余的残留和破损的包装。
笔记:我复制了此步骤中被删除的软件包列表。我知道我会再次需要它!
接下来我搜索了所有安装python2.7的地方,并删除了所有文件和目录默认 python。我希望不用手动删除它们,但别无选择:
sudo find / -iname python2.7 /etc/python2.7 /usr/local/bin/python2.7 /usr/local/lib/python2.7 /usr/lib/python2.7 /usr/bin/python2.7.13/lib/python2.7 /usr/bin/python2.7.13/bin/python2.7 /usr/bin/python2.7.13/include/python2.7 /usr/bin/python2.7 /usr/include/x86_64-linux-gnu/python2.7 /usr/include/python2.7 /usr/share/doc/python2.7 /usr/share/binfmts/python2.7 /usr/share/lintian/overrides/python2.7 /proc/sys/fs/binfmt_misc/python2.7 /var/lib/binfmts/python2.7
我没有删除上面列出的所有文件/目录,只删除了默认版本:
sudo rm -rf /etc/python2.7 /usr/local/bin/python2.7 /usr/local/lib/python2.7 /usr/lib/python2.7
还有其他版本的 Python,没有造成任何问题。我相信搜索可能因用户而异,
whereis python
可以使用命令获取默认 Python 安装的路径。接下来我安装python成功了:
sudo apt-get install python Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python-minimal python2.7 python2.7-minimal Suggested packages: python-doc python-tk python2.7-doc The following NEW packages will be installed: libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python python-minimal python2.7 python2.7-minimal 0 upgraded, 7 newly installed, 0 to remove and 21 not upgraded. Need to get 0 B/3,877 kB of archives. After this operation, 16.6 MB of additional disk space will be used. Do you want to continue? [Y/n] y Selecting previously unselected package libpython2.7-minimal:amd64. (Reading database ... 250996 files and directories currently installed.) Preparing to unpack .../libpython2.7-minimal_2.7.12-1ubuntu0~16.04.3_amd64.deb ... Unpacking libpython2.7-minimal:amd64 (2.7.12-1ubuntu0~16.04.3) ... Selecting previously unselected package python2.7-minimal. Preparing to unpack .../python2.7-minimal_2.7.12-1ubuntu0~16.04.3_amd64.deb ... Unpacking python2.7-minimal (2.7.12-1ubuntu0~16.04.3) ... Selecting previously unselected package python-minimal. Preparing to unpack .../python-minimal_2.7.12-1~16.04_amd64.deb ... Unpacking python-minimal (2.7.12-1~16.04) ... Selecting previously unselected package libpython2.7-stdlib:amd64. Preparing to unpack .../libpython2.7-stdlib_2.7.12-1ubuntu0~16.04.3_amd64.deb ... Unpacking libpython2.7-stdlib:amd64 (2.7.12-1ubuntu0~16.04.3) ... Selecting previously unselected package python2.7. Preparing to unpack .../python2.7_2.7.12-1ubuntu0~16.04.3_amd64.deb ... Unpacking python2.7 (2.7.12-1ubuntu0~16.04.3) ... Selecting previously unselected package libpython-stdlib:amd64. Preparing to unpack .../libpython-stdlib_2.7.12-1~16.04_amd64.deb ... Unpacking libpython-stdlib:amd64 (2.7.12-1~16.04) ... Processing triggers for man-db (2.7.5-1) ... Processing triggers for bamfdaemon (0.5.3~bzr0+16.04.20180209-0ubuntu1) ... Rebuilding /usr/share/applications/bamf-2.index... Processing triggers for desktop-file-utils (0.22-1ubuntu5.1) ... Processing triggers for gnome-menus (3.13.3-6ubuntu3.1) ... Processing triggers for mime-support (3.59ubuntu1) ... Setting up libpython2.7-minimal:amd64 (2.7.12-1ubuntu0~16.04.3) ... Setting up python2.7-minimal (2.7.12-1ubuntu0~16.04.3) ... Linking and byte-compiling packages for runtime python2.7... Setting up python-minimal (2.7.12-1~16.04) ... Selecting previously unselected package python. (Reading database ... 251742 files and directories currently installed.) Preparing to unpack .../python_2.7.12-1~16.04_amd64.deb ... Unpacking python (2.7.12-1~16.04) ... Processing triggers for doc-base (0.10.7) ... Processing 1 added doc-base file... Registering documents with scrollkeeper... Processing triggers for man-db (2.7.5-1) ... Setting up libpython2.7-stdlib:amd64 (2.7.12-1ubuntu0~16.04.3) ... Setting up python2.7 (2.7.12-1ubuntu0~16.04.3) ... Setting up libpython-stdlib:amd64 (2.7.12-1~16.04) ... Setting up python (2.7.12-1~16.04) ...
笔记:这仅安装了非常必要且最少的 Python 包。您可能仍需要安装在第一步中卸载的所有其他 Python 包。
一些额外说明:
第二步删除了使用以下方式全局安装的第三方 Python 包点子。不幸的是,我没有这个列表,最终我的项目需要它们时遇到了一些麻烦。但这不是什么大问题,因为我的所有项目都有自己的 python requirements.txt 文件。但我仍然建议大家在开始第一步之前将 python 包保存在一个文件中。以下命令可以派上用场:
pip freeze > python_pip_packages.txt
我还必须删除 pip 文件和目录(类似于删除 python 默认文件和目录的方式)并重新安装 pip:
sudo apt-get install python-pip # installing pip
sudo pip install --upgrade pip # upgrading pip to latest version using pip itself
sudo pip install -r python_pip_packages.txt # re-installing all 3rd party python packages which were removed in 2nd step.
希望其他人不会像我一样受苦:)
答案2
对于那些在运行命令时不断遇到困难的人(它不断抱怨python或任何其他包),我经历了同样的情况,并通过从中删除信息来解决这个问题
/var/lib/dpkg/info/<the package giving you headache>
此后,您应该可以继续安装/升级/更新