问题:python-dev安装错误:ImportError:没有名为apt_pkg的模块

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

# aptitude install python-dev

我收到以下错误:

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

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

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

# 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?


回答 0

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

apt-get install python-apt

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.

apt-get install python-apt

回答 1

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

以下代码将仅为apt_pkg....sopython 3.5重新创建文件

sudo apt-get install python3-apt --reinstall

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

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

因此,很显然,python3-apt会检查最高的python版本,而不是当前使用的python版本。

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

The following code will only re-create a apt_pkg....so file for 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.


回答 2

通过以下方法解决:

/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-35m-x86_64-linux-gnu.so apt_pkg.so

基本上,如果您No such file or directory公正ls地尝试获得正确的名字。

Solve it by this:

/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

Basically, if you get a No such file or directory just ls to try to get the right name.


回答 3

在我尝试从Deadsnakes存储库中安装Python3.7之后,在Ubuntu 18.04.2上发生了这种情况。

解决方法是这个

1) cd /usr/lib/python3/dist-packages/

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

This happened to me on Ubuntu 18.04.2 after I tried to install Python3.7 from the deadsnakes repo.

Solution was this

1) cd /usr/lib/python3/dist-packages/

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


回答 4

当同时安装了新版本的python和旧版本时,通常会发生此错误。

  • 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
    

当我们安装带有apt的非发行版python3版本时,会将共享模块目录设置为python3的共享目录,通常是/usr/lib/python3

在大多数情况下,这是可以的,但是在某些情况下,不同版本的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

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

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

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

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

  • 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
    

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.

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

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;

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.


回答 5

@ user8178061的解决方案效果很好,但是我对python3.7Ubuntu的版本做了一些修改

我更换了apt_pkg.cpython-3m-i386-linux-gnu.soapt_pkg.cpython-36m-x86_64-linux-gnu.so

这里执行两个命令:

cd /usr/lib/python3/dist-packages

sudo cp apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so

The solution of @user8178061 worked well but I did it with some modifications for my version wich is python3.7 with Ubuntu

I replaced the apt_pkg.cpython-3m-i386-linux-gnu.so with apt_pkg.cpython-36m-x86_64-linux-gnu.so

Here the two commands to execute:

cd /usr/lib/python3/dist-packages

sudo cp apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so


回答 6

在ubuntu18.04上更新python3.7之后,这对我有用

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

This worked for me on after updating python3.7 on ubuntu18.04

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

回答 7

由于某种原因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

我不确定我的升级是否中断,或者为什么会这样。尝试升级后发生(精确-> raring->定量升级)

For some reason my install was missing apt_pkg.so in the python3 dist-packages dir. (apt_pkg.cpython-33m-x86_64-linux-gnu.so was there?!) but and I had to make a symlink apt_pkg.so -> apt_pkg.cpython-33m-x86_64-linux-gnu.so in /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)


回答 8

  1. 检查您的默认Python 3版本:
python --version
Python 3.7.5
  1. cd进入/usr/lib/python3/dist-packages并检查apt_pkg.*文件。您会发现默认Python版本没有:
ll apt_pkg.*
apt_pkg.cpython-36m-x86_64-linux-gnu.so
  1. 创建符号链接:
sudo ln -s apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.cpython-37m-x86_64- linux-gnu.so 
  1. Check your default Python 3 version:
python --version
Python 3.7.5
  1. cd into /usr/lib/python3/dist-packages and check the apt_pkg.* files. You will find that there is none for your default Python version:
ll apt_pkg.*
apt_pkg.cpython-36m-x86_64-linux-gnu.so
  1. Create the symlink:
sudo ln -s apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.cpython-37m-x86_64- linux-gnu.so 

回答 9

不得已的方法是,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命令对您来说太多了,或者由于某种原因魔术无法正常工作。

cpmv如果您仅致力于使用一个Python版本,则也可以使用上述方法。

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.so if the ln command is too much for you or somehow magically doesn’t work.

cp above can also be mv if you are only dedicated to using one Python version.


回答 10

如果您使用的是python 3.7,请通过更新Alternatives将其降级为python 3.6,这对我有用

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

sudo update-alternatives --config python3

if you’re using python 3.7 downgrade it to python 3.6 by updating Alternatives, This worked for me

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

sudo update-alternatives --config python3

回答 11

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

在下面,/usr/lib/python3/dist-packages您将看到*34m*不能使用哪个python 3.5。zhazha回答了它的符号链接。

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

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


回答 12

除了为其建立符号链接外apt_pkg.so,您可能还希望以与之apt_inst.so相同的方式进行apt_pkg.so

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

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

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

回答 13

我看到每个人都在说如何通过奇怪的复制等方式修复它,但是没人真正说出为什么会出现此问题。

因此,让我解释一下,对于像我这样的人,不想仅仅因为SO上的某人告诉了他们而弄乱系统文件。


问题是:

  • 许多系统脚本都将python3 shebang硬编码到其中。您可以自己检查:
~$ grep -R "\#\!/usr/bin/python3" /usr/lib/*

/usr/lib/cnf-update-db:#!/usr/bin/python3
/usr/lib/command-not-found:#!/usr/bin/python3
/usr/lib/cups/filter/pstotiff:#!/usr/bin/python3
/usr/lib/cups/filter/rastertosag-gdi:#!/usr/bin/python3 -u
grep: /usr/lib/cups/backend/cups-brf: Permission denied
/usr/lib/cups/backend/hpfax:#!/usr/bin/python3
/usr/lib/language-selector/ls-dbus-backend:#!/usr/bin/python3
/usr/lib/python3/dist-packages/language_support_pkgs.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/softwareproperties/MirrorTest.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/cupshelpers/installdriver.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/cupshelpers/openprinting.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/cupshelpers/xmldriverprefs.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/cupshelpers/smburi.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/cupshelpers/ppds.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/cupshelpers/debug.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/DistUpgrade/dist-upgrade.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/CommandNotFound/db/creator.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/CommandNotFound/db/db.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/Quirks/quirkreader.py:#!/usr/bin/python3
grep: /usr/lib/ssl/private: Permission denied
/usr/lib/system-service/system-service-d:#!/usr/bin/python3
/usr/lib/ubuntu-release-upgrader/check-new-release-gtk:#!/usr/bin/python3
/usr/lib/ubuntu-release-upgrader/do-partial-upgrade:#!/usr/bin/python3
/usr/lib/ubuntu-release-upgrader/check-new-release:#!/usr/bin/python3
/usr/lib/update-notifier/package-data-downloader:#!/usr/bin/python3
/usr/lib/update-notifier/backend_helper.py:#!/usr/bin/python3
/usr/lib/update-notifier/apt_check.py:#!/usr/bin/python3
/usr/lib/update-notifier/apt-check:#!/usr/bin/python3

  • python apt package python-apt/python3-apt是系统软件包,因此它是默认系统python

因此,这些脚本将始终获得当前链接到的版本python3,但是由于apt软件包不存在而失败。


这也适用于python链接-如果应用程序是使用Python2编写的,但其中的某些旧语法元素在Python3中不起作用,则该应用程序将无法工作。

[我的终端打破了这种方式,因为我使用了终结者,终结者显然是用Python2.7编写的,与Python3不兼容。]


这里介绍的解决方案建议复制/链接apt软件包文件或更改python3链接。

让我们分析一下:

  1. 复制/链接apt包

应该不是问题,因为从Python3.4开始,所有python脚本也都可以在较新的版本上运行。

至今。但是,如果您将系统保留足够长的时间,将来可能会中断。

  1. python3回链接

这是一个很好的解决方案,因为我们可以回到“从不更改链接”


“但是我喜欢只打python!”– 我也喜欢这个!这就是我首先解决这个问题的方法!

  1. 通常,应该避免手动更改系统链接-update-alternatives而是使用它来链接不同的版本。这适用于具有多个版本的任何应用。这仍然会破坏那些系统脚本(因为它确实会更改链接),但是您可以轻松地来回切换,而不必担心将链接和目标按正确的顺序放置或输入错误。

  2. 考虑为链接或别名使用python/以外的其他名称python3

  3. 或将自己的python/python3链接添加到PATH(就像虚拟环境一样),而无需更改系统链接。

I see everyone saying how to fix it with strange copying etc, but no one really said why the problem occurs.

So let me explain, for those of you who like me don’t want to mess with system files only because someone on SO told them so.


The problem is that:

  • many system scripts have python3 shebang hardcoded into them. You can check it yourself:
~$ grep -R "\#\!/usr/bin/python3" /usr/lib/*

/usr/lib/cnf-update-db:#!/usr/bin/python3
/usr/lib/command-not-found:#!/usr/bin/python3
/usr/lib/cups/filter/pstotiff:#!/usr/bin/python3
/usr/lib/cups/filter/rastertosag-gdi:#!/usr/bin/python3 -u
grep: /usr/lib/cups/backend/cups-brf: Permission denied
/usr/lib/cups/backend/hpfax:#!/usr/bin/python3
/usr/lib/language-selector/ls-dbus-backend:#!/usr/bin/python3
/usr/lib/python3/dist-packages/language_support_pkgs.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/softwareproperties/MirrorTest.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/cupshelpers/installdriver.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/cupshelpers/openprinting.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/cupshelpers/xmldriverprefs.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/cupshelpers/smburi.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/cupshelpers/ppds.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/cupshelpers/debug.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/DistUpgrade/dist-upgrade.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/CommandNotFound/db/creator.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/CommandNotFound/db/db.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/Quirks/quirkreader.py:#!/usr/bin/python3
grep: /usr/lib/ssl/private: Permission denied
/usr/lib/system-service/system-service-d:#!/usr/bin/python3
/usr/lib/ubuntu-release-upgrader/check-new-release-gtk:#!/usr/bin/python3
/usr/lib/ubuntu-release-upgrader/do-partial-upgrade:#!/usr/bin/python3
/usr/lib/ubuntu-release-upgrader/check-new-release:#!/usr/bin/python3
/usr/lib/update-notifier/package-data-downloader:#!/usr/bin/python3
/usr/lib/update-notifier/backend_helper.py:#!/usr/bin/python3
/usr/lib/update-notifier/apt_check.py:#!/usr/bin/python3
/usr/lib/update-notifier/apt-check:#!/usr/bin/python3

  • python apt package python-apt/python3-apt is a system package, so it’s for default system python

Thus, the scripts will always get the version currently linked to python3, but fail because the apt package is not present.


This also applies to python link – if an app was written in Python2 with some old syntax elements that don’t work in Python3, the app will not work.

[My terminal broke that way because I use Terminator, which is apparently written in Python2.7 not compatible with Python3.]


Solutions presented here either suggest copying/linking the apt package files or changing python3 link.

Let’s analyse both:

  1. Copying/linking the apt package

This shouldn’t be a problem because from around Python3.4 all python scripts work on newer versions as well.

So far. But it may break in the future – if you keep your system long enough.

  1. Changing python3 link back

This is a great solution because we can get back to “never ever changing the link”


“But I like having to type just python!” – I like it too! That’s how I got to this problem in the first place!

  1. In general, you should avoid manually changing system links – use update-alternatives instead to link different versions. This applies to any app with many versions. This will still break those system scripts (because it does change the link), but you can switch back and forth easily, without worrying whether you put link and dest in the right order or made a typo.

  2. Consider using other name than python/python3 for your link or alias.

  3. Or add your own python/python3 link to PATH (just like virtual environments do), without changing system links.


回答 14

Windows 10 WSL v1(Ubuntu 16.04.6 LTS)

这个reddit答案(稍加修改对我有用

sudo ln -sfn /usr/lib/python3/dist-packages/apt_pkg.cpython-35m-x86_64-linux-gnu.so apt_pkg.so

Windows 10 WSL v1 (Ubuntu 16.04.6 LTS)

This reddit answer (slightly modified worked for me)

sudo ln -sfn /usr/lib/python3/dist-packages/apt_pkg.cpython-35m-x86_64-linux-gnu.so apt_pkg.so


回答 15

请查看以下文档。肯定会解决问题。 http://www.programmersought.com/article/55001874709/

Please review the following documentation. It will definitely solve the problem. http://www.programmersought.com/article/55001874709/


回答 16

没有一个答案对我有用(我正在使用Ubuntu 16.04和Python 3.6)。因此,我终于解决了以下问题:

1-连接到服务器的FTP

2-转到文件夹“ / usr / lib / python3 / dist-packages /”

3-复制文件“ apt_pkg.cpython-3 5 m-x86_64-linux-gnu.so”

4-将此重复文件重命名为“ apt_pkg.cpython-3 6 m-x86_64-linux-gnu.so”

而已!

None of the answers worked for me (I am using Ubuntu 16.04 and Python 3.6). So I finally solved the issue as following:

1- connect to the FTP of the server

2- go to the folder “/usr/lib/python3/dist-packages/”

3- duplicate the file “apt_pkg.cpython-35m-x86_64-linux-gnu.so”

4- rename this duplicated file to “apt_pkg.cpython-36m-x86_64-linux-gnu.so”

That’s it!


回答 17

我在Ubuntu 16.04上,并已升级到Python 3.7。这是我尝试添加PPA时遇到的错误

    sudo add-apt-repository ppa:ubuntu-toolchain-r/test                                           
Traceback (most recent call last):
  File "/usr/bin/add-apt-repository", line 11, in <module>
    from softwareproperties.SoftwareProperties import SoftwareProperties, shortcut_handler
  File "/usr/lib/python3/dist-packages/softwareproperties/SoftwareProperties.py", line 27, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

我可以通过创建以下符号链接来与我的初始python 3.4 apt_pkg.cpython-34m-x86_64-linux-gnu.so建立符号链接来解决此错误

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

I’m on Ubuntu 16.04, and upgraded to Python 3.7. Here is the error that I had when trying to add a PPA

    sudo add-apt-repository ppa:ubuntu-toolchain-r/test                                           
Traceback (most recent call last):
  File "/usr/bin/add-apt-repository", line 11, in <module>
    from softwareproperties.SoftwareProperties import SoftwareProperties, shortcut_handler
  File "/usr/lib/python3/dist-packages/softwareproperties/SoftwareProperties.py", line 27, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

I was able to fix this error by making symbolic link with my initial python 3.4 apt_pkg.cpython-34m-x86_64-linux-gnu.so by creating the following symbolic link

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

回答 18

请尝试通过设置区域设置变量来解决此问题:

export LC_ALL="en_US.UTF-8"

export LC_CTYPE="en_US.UTF-8"

Please try to fix this by setting the locale variables:

export LC_ALL="en_US.UTF-8"

export LC_CTYPE="en_US.UTF-8"

回答 19

以防万一,这终于解决了这个问题,这显然是python版本冲突所致,方法是重定向链接python3,然后将其重定向到正确的python版本:

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

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

python3 -V

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:

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

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

python3 -V

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。