如何解决“ModuleNotFoundError:没有名为'apt_pkg'的模块”和“E:子进程返回错误代码”

问题描述 投票:0回答:2

我使用的是 Pop-os 22.04,我将默认 Python 从 3.10 更新到了 3.11。更新 python 后,每次尝试执行 “sudo apt update”

时,我都会收到此错误
$ sudo apt update
**Note: Skiped first few lines, those were not errors.**

Traceback (most recent call last):
  File "/usr/lib/cnf-update-db", line 3, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
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

如果我将默认的 python 版本更改回 3.10,则不会再出现此错误。 我这样做是为了更改我的 python 版本:

$ sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                 Priority   Status
------------------------------------------------------------
  0            /usr/bin/python3.11   2         auto mode
* 1            /usr/bin/python3.10   1         manual mode
  2            /usr/bin/python3.11   2         manual mode

Press <enter> to keep the current choice[*], or type selection number: 2

非常抱歉我的英语不好

apt ubuntu-22.04 python-3.11
2个回答
0
投票

cd /usr/lib/python3/dist-packages; sudo ln -s apt_pkg.cpython-3[67]m-x86_64-linux-gnu.so apt_pkg.so


0
投票

最好的解决办法就是回避问题。不要尝试在 Pop-os 中运行 3.11 作为默认 python。在 Pop-os 中切换回 3.10。如果您需要 python 3.11,请使用 pyenv 安装它(https://github.com/pyenv/pyenv#set-up-your-shell-environment-for-pyenv),但让 3.10 保留为 Pop 的默认 python -os 直到 Pop-os 团队决定更新到 3.11。

© www.soinside.com 2019 - 2024. All rights reserved.