Linux python-dev 安装错误:ImportError: No module named apt_pkg

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13708180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:57:27  来源:igfitidea点击:

python-dev installation error: ImportError: No module named apt_pkg

pythonlinuxinstallationdebian

提问by Belphegor

I am Debian user, and I want to install python-dev, but when I run the code in the shell as a root:

我是 Debian 用户,我想安装 python-dev,但是当我以 root 身份在 shell 中运行代码时:

# aptitude install python-dev

I get the following error:

我收到以下错误:

Traceback (most recent call last):       
  File "/usr/bin/apt-listchanges", line 28, in <module>
    import apt_pkg
ImportError: No module named apt_pkg

What seems to be the problem and how can I resolve it?

似乎是什么问题,我该如何解决?

采纳答案by Arnestig

Make sure you have a working python-apt package. You could try and remove and install that package again to fix the problem with apt_pkg.so not being located.

确保你有一个可用的 python-apt 包。您可以尝试再次删除并安装该软件包以解决无法定位 apt_pkg.so 的问题。

apt-get install python-apt

回答by Jamie Pate

For some reason my install was missing apt_pkg.soin the python3 dist-packages dir. (apt_pkg.cpython-33m-x86_64-linux-gnu.sowas there?!) but and I had to make a symlink apt_pkg.so -> apt_pkg.cpython-33m-x86_64-linux-gnu.soin /usr/lib/python3/dist-packages

出于某种原因,我的安装apt_pkg.so在 python3 dist-packages 目录中丢失了。(apt_pkg.cpython-33m-x86_64-linux-gnu.so在那里吗?!)但是我必须apt_pkg.so -> apt_pkg.cpython-33m-x86_64-linux-gnu.so/usr/lib/python3/dist-packages

I'm not sure whether my upgrade was broken or why this was the case. It occured after trying to upgrade (precise->raring->quantal upgrade)

我不确定我的升级是否已损坏或为什么会出现这种情况。它在尝试升级后发生(精确-> 稀有-> 量子升级)

回答by zhazha

I met this problem when doing sudo apt-get update. My env is debian8, with python2.7 + 3.4(default) + 3.5.

我在做的时候遇到了这个问题sudo apt-get update。我的环境是 debian8,python2.7 + 3.4(默认)+ 3.5。

The following code will only re-create a apt_pkg....sofile for python 3.5

以下代码只会apt_pkg....so为python 3.5重新创建一个文件

sudo apt-get install python3-apt --reinstall

The following code solved my problem,

以下代码解决了我的问题,

cd /usr/lib/python3/dist-packages
sudo ln -s apt_pkg.cpython-{35m,34m}-x86_64-linux-gnu.so

So, obviously, python3-apt checks the highest python version, instead of the current python version in use.

所以,很明显,python3-apt 会检查最高的 python 版本,而不是当前使用的 python 版本。

回答by user8178061

Solve it by this:

用这个解决:

/usr/lib/python3/dist-packages# cp apt_pkg.cpython-34m-i386-linux-gnu.so apt_pkg.so

/usr/lib/python3/dist-packages# cp apt_pkg.cpython-34m-i386-linux-gnu.so apt_pkg.so

Or:

或者:

/usr/lib/python3/dist-packages# cp apt_pkg.cpython-35m-x86_64-linux-gnu.so apt_pkg.so

/usr/lib/python3/dist-packages# cp apt_pkg.cpython-35m-x86_64-linux-gnu.so apt_pkg.so

Basically, if you get a No such file or directoryjust lsto try to get the right name.

基本上,如果你得到一个No such file or directory只是ls为了得到正确的名字。

回答by Pobe

If you're using python 3.5, downgrade to 3.4. That's the safest move to do.

如果您使用的是 python 3.5,请降级到 3.4。这是最安全的举动。

Under /usr/lib/python3/dist-packagesyou'll see *34m*which python 3.5 can't use. zhazha answer symlink to it.

下面/usr/lib/python3/dist-packages你会看到*34m*哪个python 3.5不能用。zazha 回答符号链接到它。

回答by matthias2t

Just in case it helps another, I finally solved this problem, that was apparently caused by python version conflicts, by redirecting the link python3, then redirecting it to the right python version:

以防万一它帮助另一个,我终于解决了这个问题,这显然是由python版本冲突引起的,通过重定向链接python3,然后将其重定向到正确的python版本:

sudo rm /usr/bin/python3
sudo ln -s /usr/bin/python3.4

You may need to enter the correct python version, found with:

您可能需要输入正确的 python 版本,可以通过以下方式找到:

python3 -V

回答by Tong Niu

A last resort is sudo cp /usr/lib/python3/dist-packages/apt_pkg.cpython-35m-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/apt_pkg.cpython-36m-x86_64-linux-gnu.soif the lncommand is too much for you or somehow magically doesn't work.

最后的办法是sudo cp /usr/lib/python3/dist-packages/apt_pkg.cpython-35m-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/apt_pkg.cpython-36m-x86_64-linux-gnu.so如果ln命令对你来说太多了,或者不知何故神奇地不起作用。

cpabove can also be mvif you are only dedicated to using one Python version.

cpmv如果您只专注于使用一个 Python 版本,也可以使用上述方法。

回答by Jihwan Park

In addition to making a symbolic link for apt_pkg.so, you may want to make apt_inst.soin the same manner of apt_pkg.so.

除了为 制作符号链接之外apt_pkg.so,您可能还想以apt_inst.so相同的方式制作apt_pkg.so.

ln -s apt_inst.cpython-35m-x86_64-linux-gnu.so apt_inst.so 

回答by Jamie Lindsey

This error will often occur when a newer version of python has been installed alongside an older version e.g;

当较新版本的 python 与旧版本一起安装时,通常会发生此错误,例如;

  • Ubuntu 18.04.1 ships with python version 3.6.6
  • Installed ppa:deadsnakes/python3.7.1 or alternative
  • Run a command that uses the apt_pkg module and get an error such as;

        from CommandNotFound.db.db import SqliteDatabase
    File "/usr/lib/python3/dist-packages/CommandNotFound/db/db.py", line 5, in <module>
        import apt_pkg
    
  • Ubuntu 18.04.1 附带 Python 版本 3.6.6
  • 已安装 ppa:deadsnakes/python3.7.1 或替代品
  • 运行一个使用 apt_pkg 模块的命令并得到一个错误,例如;

        from CommandNotFound.db.db import SqliteDatabase
    File "/usr/lib/python3/dist-packages/CommandNotFound/db/db.py", line 5, in <module>
        import apt_pkg
    

When we install a non-distro python3 version with apt it will set a shared module directory to be that of python3 most usually it will be /usr/lib/python3.

当我们使用 apt 安装非发行版 python3 版本时,它会将共享模块目录设置为 python3 的目录,最常见的是/usr/lib/python3.

Most of the time this will be ok, but under some circumstances the different versions of python rely on different libraries or shared objects/libraries than the other python version does, so as other answers have pointed out we need to link the .SO to the correct python version. So if we have python3.6 installed on a 64bit system then the apt_pkg .SO link would be

大多数情况下这没问题,但在某些情况下,不同版本的 python 依赖于不同的库或共享对象/库,而不是其他 python 版本,因此其他答案指出我们需要将 .SO 链接到正确的python版本。因此,如果我们在 64 位系统上安装了 python3.6,那么 apt_pkg .SO 链接将是

sudo ln -s apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so

But the problem lies in the fact that when we install a newer python version the link will update to point to the newest python version, which leads to the error of apt_pkg module not being found. By checking which version of python ships with your distro you can create the link as shown above. Or we use a method to offer the command a choice of python versions to link the .SO such as;

但是问题在于,当我们安装较新的python版本时,链接会更新为指向最新的python版本,从而导致找不到apt_pkg模块的错误。通过检查发行版附带的 python 版本,您可以创建如上所示的链接。或者我们使用一种方法为命令提供python版本的选择来链接.SO,例如;

sudo ln -s apt_pkg.cpython-{36m,35m,34m}-x86_64-linux-gnu.so apt_pkg.so

Because python will create this link to the newest installed python version we give the command the option to choose from 3 python versions, of which it will choose the highest version given.

因为 python 会创建这个指向最新安装的 python 版本的链接,我们给命令提供了从 3 个 python 版本中进行选择的选项,其中它将选择给定的最高版本。

回答by rajendra sharma

Please try to fix this by setting the locale variables:

请尝试通过设置语言环境变量来解决此问题:

export LC_ALL="en_US.UTF-8"

export LC_CTYPE="en_US.UTF-8"