34
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2018-04-30

前置き

急に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

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

34
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?