升级Ubuntu 16.04 LTS python-minimal包错误时出错

一切都非常顺利,直到今天我决定将Ubuntu 16.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  import struct File "/usr/local/lib/python2.7/struct.py", line 1, in  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  import struct File "/usr/local/lib/python2.7/struct.py", line 1, in  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 

有没有人面临类似的问题,并能够解决它? 任何帮助将不胜感激。

经过几个小时的挣扎和数百次安装/卸载python包后,我能够安装python包,包括python-minimal 。 让我分享一下我的成功故事。

  1. 一开始,我甚至无法卸载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' ' ') 

    如果有任何残留和borken包去除。

    注意我复制了在此步骤中删除的软件包列表。 我知道我会再次需要它!

  2. 接下来,我搜索了安装了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的路径。

  3. 接下来,我成功安装了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包。 您可能仍需要安装在第1步中卸载的所有其他python包。

几个额外的笔记:

第二步删除了第三方python包,它们是使用pip全局安装的。 不幸的是,我没有列表,最终我的项目需要它们几个麻烦。 但这并不是什么大问题,因为我的所有项目都有自己的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. 

希望别人不要像我一样受苦:)

对于那些在运行命令时不断陷入困境的人(它经常抱怨python或任何其他软件包),我经历了同样的事情并通过从中删除信息来排序

 /var/lib/dpkg/info/ 

之后,应该允许您继续安装/升级/更新