ModuleNotFoundError:sudo apt-get update 时没有名为“apt_pkg”的模块

ModuleNotFoundError:sudo apt-get update 时没有名为“apt_pkg”的模块

出现此错误之前:我执行了以下步骤

sudo apt-get install python3.7
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1 
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
sudo update-alternatives --config python3
alias python=python3

这是我尝试更新 sudo apt-get update 时的错误消息

browser@virtual-machine:/usr/lib/python3/dist-packages$ sudo apt-get update
Hit:1 http://no.archive.ubuntu.com/ubuntu bionic InRelease
Hit:2 http://no.archive.ubuntu.com/ubuntu bionic-updates InRelease                                                                          
Hit:3 https://download.docker.com/linux/ubuntu bionic InRelease                                                                             
Hit:4 http://no.archive.ubuntu.com/ubuntu bionic-backports InRelease                                                                        
Ign:5 http://dl.google.com/linux/chrome/deb stable InRelease                                                                                
Hit:6 http://dl.google.com/linux/chrome/deb stable Release                                                                                  
Get:7 http://security.ubuntu.com/ubuntu bionic-security InRelease [88,7 kB]                                                                 
Hit:8 http://apt.postgresql.org/pub/repos/apt bionic-pgdg InRelease                   
Fetched 88,7 kB in 1s (133 kB/s)                                                     
Traceback (most recent call last):
  File "/usr/lib/cnf-update-db", line 8, in <module>
    from CommandNotFound.db.creator import DbCreator
  File "/usr/lib/python3/dist-packages/CommandNotFound/db/creator.py", line 11, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
    import apport.fileutils
  File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
    from apport.packaging_impl import impl as packaging
  File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 24, in <module>
    import apt
  File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

Original exception was:
Traceback (most recent call last):
  File "/usr/lib/cnf-update-db", line 8, in <module>
    from CommandNotFound.db.creator import DbCreator
  File "/usr/lib/python3/dist-packages/CommandNotFound/db/creator.py", line 11, 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

答案1

解决方案是将 Python 替代方案切换为 3.6,而不是 3.7。因为 Python 3.7 显然破坏了“sudo apt-get update”

答案2

以下对我有用:

cd  /usr/lib/python3/dist-packages
ls -la /usr/lib/python3/dist-packages
sudo cp apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so

或者:

sudo cp apt_pkg.cpython-36m-arm-linux-gnueabihf.so apt_pkg.so

取决于您的平台。干杯!

相关内容