Ubuntu 服务器上的 apt 和 python 损坏:XXX 歧义

Ubuntu 服务器上的 apt 和 python 损坏:XXX 歧义

运行 Ubuntu 服务器 20.04LTS 的unattended-upgrades似乎破坏了 python 安装,随后 也破坏了apt。这使我无法升级或安装软件包。它还破坏了依赖于 python 的服务。

首先注意到的是sudo apt update

$ sudo apt update
Hit:1 http://ports.ubuntu.com/ubuntu-ports focal InRelease
Hit:2 http://ports.ubuntu.com/ubuntu-ports focal-updates InRelease
Hit:3 http://ports.ubuntu.com/ubuntu-ports focal-backports InRelease
Hit:4 http://ports.ubuntu.com/ubuntu-ports focal-security InRelease
Fatal Python error: init_import_size: Failed to import the site module
Python runtime state: initialized
Traceback (most recent call last):
  File "/usr/lib/python3.8/site.py", line 175, in addpackage
    exec(line)
  File "<string>", line 1
    import sys, types, os;has_mfs = sys.version_info > (3, 5);p = os.path.join(sys._getframe(1).f_locals['sitedir'], *('zope',));importlib = has_mfs and __import__('importlib.util');has_mfs and __import__('importlib.machinery');m = has_mfs and sys.modules.setdefault('zope', importlib.util.module_from_spec(importlib.machinery.PathFinder.find_spec('zope', [os.path.dirname(p)])));m = m or sys.modules.setdefault('zope', types.ModuleType('zope'));mp = (m or []) and m.__dict__.setdefault('__path__',[]);(p not in mp) and mp.append(p)
    ^
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.8/site.py", line 597, in <module>
    main()
  File "/usr/lib/python3.8/site.py", line 584, in main
    known_paths = addsitepackages(known_paths)
  File "/usr/lib/python3.8/site.py", line 367, in addsitepackages
    addsitedir(sitedir, known_paths)
  File "/usr/lib/python3.8/site.py", line 214, in addsitedir
    addpackage(sitedir, name, known_paths)
  File "/usr/lib/python3.8/site.py", line 183, in addpackage
    print("Error processing line {:d} of {}:\n".format(n+1, fullname),
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x85 in position 0: invalid start byte
Reading package lists... Done
E: Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/lib/command-not-found/ -a -e /usr/lib/cnf-update-db; then /usr/lib/cnf-update-db > /dev/null; fi'
E: Sub-process returned an error code

Python3 似乎已损坏:粘贴箱

$ python3
XXX ambiguity!
XXX ambiguity!
XXX ambiguity!
<65 lines repetition>
...
XXX too high nonterminal number!
XXX too high nonterminal number!
XXX too high nonterminal number!
<40 lines repetition>
Fatal Python error: init_import_size: Failed to import the site module
Python runtime state: initialized
Traceback (most recent call last):
  File "/usr/lib/python3.8/site.py", line 175, in addpackage
    exec(line)
  File "<string>", line 1
    import sys, types, os;has_mfs = sys.version_info > (3, 5);p = os.path.join(sys._getframe(1).f_locals['sitedir'], *('zope',));importlib = has_mfs and __import__('importlib.util');has_mfs and __import__('importlib.machinery');m = has_mfs and sys.modules.setdefault('zope', importlib.util.module_from_spec(importlib.machinery.PathFinder.find_spec('zope', [os.path.dirname(p)])));m = m or sys.modules.setdefault('zope', types.ModuleType('zope'));mp = (m or []) and m.__dict__.setdefault('__path__',[]);(p not in mp) and mp.append(p)
    ^
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.8/site.py", line 597, in <module>
    main()
  File "/usr/lib/python3.8/site.py", line 584, in main
    known_paths = addsitepackages(known_paths)
  File "/usr/lib/python3.8/site.py", line 367, in addsitepackages
    addsitedir(sitedir, known_paths)
  File "/usr/lib/python3.8/site.py", line 214, in addsitedir
    addpackage(sitedir, name, known_paths)
  File "/usr/lib/python3.8/site.py", line 183, in addpackage
    print("Error processing line {:d} of {}:\n".format(n+1, fullname),
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x85 in position 0: invalid start byte

然而,python3 --version works

$ python3 --version
Python 3.8.10

以下软件包被标记为需要重新安装dpkg

$ sudo dpkg -l | grep ^..R
pFR cloud-init                     21.4-0ubuntu1~20.04.1             all          initialization and customization tool for cloud instances
rFR landscape-common               19.12-0ubuntu4.2                  arm64        Landscape administration system client - Common files
rFR python3-apport                 2.20.11-0ubuntu27.21              all          Python 3 library for Apport crash report handling
rFR python3-commandnotfound        20.04.5                           all          Python 3 bindings for command-not-found.
iFR python3-distupgrade            1:20.04.37                        all          manage release upgrades
iFR python3-problem-report         2.20.11-0ubuntu27.21              all          Python 3 library to handle problem reports
rFR sosreport                      4.2-1ubuntu0.20.04.1              arm64        Set of tools to gather troubleshooting data from a system
rFR ubuntu-advantage-tools         27.6~20.04.1                      arm64        management tools for Ubuntu Advantage

但无法使用 apt 安装任何这些包。

尝试过:

  • sudo dpkg --configure -a-> 控制台日志:粘贴箱
  • 甚至lsb_release -a不起作用:粘贴箱

原始帖子:https://stackoverflow.com/q/72339370/3125466
这个问题属于这里,因此原帖已被关闭

答案1

很明显,python 二进制文件/库已损坏,因为 python3 解释器的执行导致了错误。

Ubuntu 20.04 附带 python3.8,并且是许多软件包(包括 ubuntu 服务器本身)的依赖项。这意味着 python3.8 无法删除并重新安装,至少会产生后果。

解决方案是安装 python3 的替代版本,在本例中为 3.9。符号链接python3python3.8python3.9(因为 python3.8 在许多系统路径中是硬编码的,因此会出现错误)。

/usr/bin/

aarch64-linux-gnu-python3-config -> aarch64-linux-gnu-python3.9-config
python3 -> python3.9
python3.8 -> python3.9
python3.9-config -> aarch64-linux-gnu-python3.9-config

/usr/lib

python3.8 -> python3.9

dpkg -C此后,重新配置了除 之外列出的所有软件包python3.8。这已经使一切正常运行。

还有一个小问题,它apt会抱怨python3.8没有配置。可以通过以下方法解决这个答案

相关内容