简体   繁体   English

如何修复'ModuleNotFoundError:没有名为'apt_pkg'的模块?

[英]How to fix 'ModuleNotFoundError: No module named 'apt_pkg'?

I'm running code sudo apt update and fetch error我正在运行代码sudo apt update并获取错误

ModuleNotFoundError: No module named 'apt_pkg'. ModuleNotFoundError:没有名为“apt_pkg”的模块。

Please, help me solve it.请帮我解决它。

sudo apt update


Hit:20 http://ppa.launchpad.net/xorg-edgers/ppa/ubuntu bionic InRelease       
Fetched 93,3 kB in 2s (42,4 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'
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

Just reinstalled python3-apt and the error disappeared刚刚重新安装了python3-apt,错误消失了

sudo apt remove python3-apt

restart and then install,重启然后安装,

sudo apt install python3-apt

NOTE: Use only if, python3 is a new installation dependency.注意:仅在 python3 是新的安装依赖项时使用。 Else it can screw existing python3 based setups on your system.否则,它可能会破坏您系统上现有的基于 python3 的设置。 Use of --reinstall is a safer bet though.不过,使用--reinstall是一个更安全的选择。

In my case the problem was due to upgrading python version from 3.6 to 3.8 .在我的情况下,问题是由于将 python 版本从3.6升级到3.8

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 2

sudo update-alternatives --config python3

Solved by:解决者:

Settings back the python version to 3.6将 python 版本设置回3.6

In my case, the problem was that I removed original /usr/bin/python3 symlink on Ubuntu 18.04 and replaced it with one pointing to python3.8 .就我而言,问题是我删除了 Ubuntu 18.04 上的原始/usr/bin/python3符号链接,并将其替换为指向python3.8的符号链接。 Problem disappeared when I restored the original pointing to python3.6当我恢复原来指向python3.6时问题消失了

My takeaway is: if you need custom version of some library or module, install it in an isolated environment, do not mess up with system settings.我的收获是:如果您需要某些库或模块的自定义版本,请将其安装在隔离环境中,不要弄乱系统设置。 Otherwise you are at risk of breaking something which can be noticed only later when it's difficult to figure out what exactly is wrong.否则,您可能会破坏某些东西,而这些东西只有在以后很难弄清楚到底是什么错误时才能注意到。

我遇到了这个问题,这是因为我安装了 python 3.6.9 和 3.5.2(Ubuntu 16.04)我还使用了sudo update-alternatives --config python3不使用auto设置它来auto为我解决了这个问题(然后给予 3.6.9 优先级是让最新版本的 python 默认运行的下一步)新用户:我无法回复其他评论 :(

The issue is caused by setting python3.7 or higher as default python3.该问题是由将 python3.7 或更高版本设置为默认 python3 引起的。 @bluevalien like you said i had to use sudo update-alternatives --config pyhton3 and i set python3.6 as default. @bluevalien 就像你说的我必须使用sudo update-alternatives --config pyhton3并且我将 python3.6 设置为默认值。 Sorry i cannot comment since i'm a new user too抱歉,我也是新用户,无法发表评论

You need change the Python version (3.6 works for me).您需要更改 Python 版本(3.6 适合我)。 But, if you want another python version by default (3.8 for example):但是,如果您默认需要另一个 python 版本(例如 3.8):

Try editing the file /usr/bin/add-apt-repository and changing out the line #!/usr/bin/python3 with the specific version of python (like 3.6).尝试编辑文件/usr/bin/add-apt-repository并使用特定版本的 python(如 3.6)更改#!/usr/bin/python3行。

To change this file, open a text editor (like nano):要更改此文件,请打开文本编辑器(如 nano):

sudo nano /usr/bin/add-apt-repository

Replace #!/usr/bin/python3 for your python version (3.6 in this case)#!/usr/bin/python3替换为您的 python 版本(在这种情况下为 3.6)

#!/usr/bin/python3.6

Save the file.保存文件。

Should work.应该管用。

I also had the issue with "cp: cannot stat 'apt_pkg.cpython-36m-x86_64-linux-gnu.so': No such file or directory while I was as super user. I did the same command withouth the sudo command and it worked fine.我也遇到了“cp:无法统计'apt_pkg.cpython-36m-x86_64-linux-gnu.so':当我作为超级用户时没有这样的文件或目录的问题。我在没有sudo命令的情况下执行了相同的命令工作正常。

I have in my system python 3.5, 3.6 and 3.7 , so I do not think the version of python is a problem.我的系统中有 python 3.5、3.6 和 3.7 ,所以我认为 python 的版本不是问题。

cp apt_pkg.cpython-36m-x86-64-linux-gnu.so.so apt_pkg.so

Regards.问候。

24.12.2022 24.12.2022

i also got this error when almost run any command, it shows that the file in几乎运行任何命令时我也遇到此错误,它显示文件在

/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py

couldnt import apt_pkg无法导入 apt_pkg

there could be several reasons that cause this prob., but i'd suggest not to directly remove and reinstall python3-apt, could be problematic, the first try could be check if you have updated the default python3 to a higher version before, like by me, i've updated the default python3 to python3.10, and that's why this error comes up.可能有几个原因导致这个问题,但我建议不要直接删除并重新安装python3-apt,可能会有问题,第一次尝试可以检查你之前是否将默认的python3更新到更高版本,比如我已经将默认的 python3 更新为 python3.10,这就是出现此错误的原因。 Run跑步

sudo update-alternatives --config python3

to rechoose the default, by selecting the old one(python3.8 by me), the "no module named apt_pkg" error has been eliminated.重新选择默认值,通过选择旧的(我选择 python3.8),“没有名为 apt_pkg 的模块”错误已被消除。

Btw, a tip also a memo for myself, next time when someone wanna run process on new python environment, it could be very safe and nice to just create a virtual env using conda, no conflict with the current OS(Ubuntu 20.04 by me for example).顺便说一句,一个提示也是我自己的一个备忘录,下次有人想在新的 python 环境中运行进程时,使用 conda 创建一个虚拟环境可能非常安全和好,与当前操作系统没有冲突(我的 Ubuntu 20.04例子)。

Similar to some, I have python3.8 and python3.11 where python and python3 both are symlinks to python3.11 in /usr/bin与某些类似,我有 python3.8 和 python3.11,其中 python 和 python3 都是 /usr/bin 中 python3.11 的符号链接

I ran the following which fixed the issue:我运行了以下解决问题的命令:

cd /usr/lib/python3/dist-packages/
sudo cp apt_pkg.cpython-38-x86_64-linux-gnu.so apt_pkg.so

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM