Help us understand the problem. What is going on with this article?

add-apt-repositoryでModuleNotFoundError: No module named 'apt_pkg'

More than 1 year has passed since last update.

前置き

急にadd-apt-repositoryが動かなくなった。

エラーメッセージ

sudo apt-add-repository ppa:ansible/ansible -y
Traceback (most recent call last):
  File "/usr/bin/apt-add-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'
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 23, 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/bin/apt-add-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'
  • apt_pkgというモジュールが見つからなかった
  • python3で動いている

ということがエラーメッセージからわかる。
また、ここから、「ちょっと前に入れたPython3.6が悪さをしてるのでは」という推測を立てた(当時のデフォルトはPython3.5)。

apt_pkgモジュールの捜索

調べてみると、/usr/lib/python3/dist-packages/apt_pkg.cpython-35m-x86_64-linux-gnu.soというファイルは存在していた。
sys.pathにも/usr/lib/python3/dist-packages/は入っているみたいだし、import apt_pkgとすればこれが読み込まれるはず。
なぜ読み込まれないのか考えてみた。
35mという部分が気になった。
Python3.5を表してそう。

Python3.6用に移植

ということで、Python3.6用の名前でシンボリックリンクを張ってみた。

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

動いた

sudo apt-add-repository ppa:ansible/ansible -y
gpg: keyring `/tmp/tmpsynejl3d/secring.gpg' created
gpg: keyring `/tmp/tmpsynejl3d/pubring.gpg' created
gpg: requesting key 7BB9C367 from hkp server keyserver.ubuntu.com
gpg: /tmp/tmpsynejl3d/trustdb.gpg: trustdb created
gpg: key 7BB9C367: public key "Launchpad PPA for Ansible, Inc." imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
OK

エピローグ

/usr/bin/python -> /usr/bin/python3.6
/usr/bin/python3 -> /usr/bin/python3.6

になっていたので、

sudo rm -rf /usr/bin/python
sudo ln -s /usr/bin/python3.5 /usr/bin/python

かもしくは

sudo rm -rf /usr/bin/python3
sudo ln -s /usr/bin/python3.5 /usr/bin/python3

としても動きそう(未確認)。

miyagi1024
SoftwareDvelopment, LinuxKernelReading, MalwareAnalysis, Exploit, AtCoder / seccamp'17'18, SecHack365'18, GlobalCybersecurityCamp'18
https://miyagi1024.github.io
ipfactory
メンバーが各々の技術分野を追求するサークル、「IPFactory」のOrganizationです。それぞれのアウトプット活動を促進するために発足されました。
https://twitter.com/_ipfactory_
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした