apt update 실행 시 오류 발생: ModuleNotFoundError: 'apt_pkg'라는 모듈이 없습니다.

apt update 실행 시 오류 발생: ModuleNotFoundError: 'apt_pkg'라는 모듈이 없습니다.

내가 달릴 apt update우분투 18.04.6 LTS(우리 회사에서 Netbox를 호스팅하기 위해 사용하는 서버) 다음과 같은 문제가 발생합니다.

$ sudo apt update
Hit:1 http://azure.archive.ubuntu.com/ubuntu bionic InRelease
Hit:2 http://azure.archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:3 http://azure.archive.ubuntu.com/ubuntu bionic-backports InRelease
Hit:4 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic InRelease
Hit:5 https://packages.microsoft.com/ubuntu/18.04/prod bionic InRelease
Hit:6 http://security.ubuntu.com/ubuntu bionic-security InRelease
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'

다음을 실행할 때 비슷한 문제가 발생합니다 neofetch.

$ neofetch
Traceback (most recent call last):
  File "/usr/lib/command-not-found", line 28, in <module>
    from CommandNotFound import CommandNotFound
  File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 19, in <module>
    from CommandNotFound.db.db import SqliteDatabase
  File "/usr/lib/python3/dist-packages/CommandNotFound/db/db.py", line 5, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

내 동료가 이것을 실행하고 다시 시도하라고 말했지만 동일한 문제가 발생했습니다.

sudo apt-get remove command-not-found
sudo apt-get clean
sudo apt-get update
sudo apt-get install command-not-found

그런 다음 이 서버에 몇 개의 Python 버전이 설치되어 있는지 확인하러 갔더니 4개를 셉니다.

$ ls -ls /usr/bin/python*
   0 lrwxrwxrwx 1 root root       9 Apr 16  2018 /usr/bin/python -> python2.7
   0 lrwxrwxrwx 1 root root       9 Apr 16  2018 /usr/bin/python2 -> python2.7
3544 -rwxr-xr-x 1 root root 3628904 Nov 28 18:51 /usr/bin/python2.7
   0 lrwxrwxrwx 1 root root      25 Jan 10 08:10 /usr/bin/python3 -> /etc/alternatives/python3
   0 lrwxrwxrwx 1 root root      16 Oct 25  2018 /usr/bin/python3-config -> python3.6-config
   4 -rwxr-xr-x 1 root root    1018 Oct 28  2017 /usr/bin/python3-jsondiff
   4 -rwxr-xr-x 1 root root    3661 Oct 28  2017 /usr/bin/python3-jsonpatch
   4 -rwxr-xr-x 1 root root    1342 May  1  2016 /usr/bin/python3-jsonpointer
   4 -rwxr-xr-x 1 root root     398 Nov 15  2017 /usr/bin/python3-jsonschema
5356 -rwxr-xr-x 1 root root 5481816 Dec  7 01:11 /usr/bin/python3.10
4424 -rwxr-xr-x 2 root root 4526456 Nov 25 14:10 /usr/bin/python3.6
   0 lrwxrwxrwx 1 root root      33 Nov 25 14:10 /usr/bin/python3.6-config -> x86_64-linux-gnu-python3.6-config
4424 -rwxr-xr-x 2 root root 4526456 Nov 25 14:10 /usr/bin/python3.6m
   0 lrwxrwxrwx 1 root root      34 Nov 25 14:10 /usr/bin/python3.6m-config -> x86_64-linux-gnu-python3.6m-config
   0 lrwxrwxrwx 1 root root      10 Oct 25  2018 /usr/bin/python3m -> python3.6m
   0 lrwxrwxrwx 1 root root      17 Oct 25  2018 /usr/bin/python3m-config -> python3.6m-config

실행하면 python3 --version반환됩니다.Python 3.10.9

호스팅된 앱을 마이그레이션하지 않고도 이 문제를 어떻게 해결할 수 있는지 아는 사람이 있나요? 그건 악몽이겠죠 :(

편집하다:

via를 /usr/lib/python3/dist-packages찾아서 파일을 찾았습니다 . 파일 이름을 다음으로 바꾸 었고 이제 새로운 오류 메시지와 함께 실행할 수 있습니다 .apt_pkgls -la | grep "apt_pkg.cpython"apt_pkg.cpython-36m-x86_64-linux-gnu.soapt_pkg.soapt update

$ sudo apt update && sudo apt upgrade
[sudo] password for admin-federico-dostuni:
Hit:1 http://azure.archive.ubuntu.com/ubuntu bionic InRelease
Get:2 http://azure.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:3 http://azure.archive.ubuntu.com/ubuntu bionic-backports InRelease [83.3 kB]
Hit:4 http://security.ubuntu.com/ubuntu bionic-security InRelease
Hit:5 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic InRelease
Hit:6 https://packages.microsoft.com/ubuntu/18.04/prod bionic InRelease
Fetched 172 kB in 1s (176 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
1 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] y

Setting up update-notifier-common (3.192.1.15) ...
Traceback (most recent call last):
  File "/usr/lib/update-notifier/package-data-downloader", line 24, in <module>
    import debian.deb822
  File "/usr/lib/python3/dist-packages/debian/deb822.py", line 78, in <module>
    class TagSectionWrapper(collections.Mapping):
AttributeError: module 'collections' has no attribute 'Mapping'
dpkg: error processing package update-notifier-common (--configure):
 installed update-notifier-common package post-installation script subprocess returned error exit status 1
Errors were encountered while processing:
 update-notifier-common
E: Sub-process /usr/bin/dpkg returned an error code (1)

답변1

file 때문에 /usr/lib/update-notifier/package-data-downloader첫 번째 줄에는 다음이 포함됩니다.

#!/usr/bin/python3


미리 정보를 보면, 당신의 파이썬은 Python 3.10.9,

apt_pkg는 apt_pkg.cpython-36m-x86_64-linux-gnu.sopython3.6에만 일치한다는 의미입니다. 다음을 수행해야 합니다.

sudo rm /usr/bin/python3

sudo ln -f /usr/bin/python3.6 /usr/bin/python3

python3 --version있는지 확인하세요python3.6.x

테스트를 위해 업데이트 명령을 실행하고,

sudo apt update

답변2

우분투 22.04.

시도할 때 계속 경고와 오류 메시지가 표시되었습니다 apt update. 세부 사항은 다음과 같습니다.

Fetched 16.8 kB in 1s (14.2 kB/s) sh: 1: /usr/lib/cnf-update-db: not found Reading package lists... Done W: An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: https://dl.google.com/linux/chrome/deb stable InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY E88979FB9B30ACF2 W: Failed to fetch https://dl.google.com/linux/chrome/deb/dists/stable/InRelease The following signatures couldn't be verified because the public key is not available: NO_PUBKEY E88979FB9B30ACF2 W: Some index files failed to download. They have been ignored, or old ones used instead. 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

/usr/bin/python3해당 주소 는 무효 주소로 연결되어 있는 것으로 밝혀졌습니다 . 로 확인해 보니 ls -ls /usr/bin/python*사용할 수 없는 것을 나타내는 빨간색 선이 있습니다.

내가 유용하다고 생각한 것은 이 링크를 ln -s /usr/bin/pythonVERSIONAVAILABLE /usr/bin/python3.

해결되었습니다.

관련 정보