RTC222
RTC222

Reputation: 2323

ModuleNotFoundError: No module named 'apt_pkg' installing deadsnakes repository

I want to install Python 3.10 on Ubuntu 18.04 (I'm currently on Python 3.8) from the deadsnakes repository with the following set of commands I found on the internet:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.10

But I got the error sudo: add-apt-repository: command not found.

More net research led me to this set of commands at "ModuleNotFoundError: No module named 'apt_pkg'" appears in various commands - Ask Ubuntu:

sudo apt remove python3-apt
sudo apt autoremove
sudo apt autoclean
sudo apt install python3-apt

Other web sources said the same thing, so I did that, but I still get the error message when I run sudo add-apt-repository ppa:deadsnakes/ppa.

Then I found How to Fix 'add-apt-repository command not found' on Ubuntu & Debian - phoenixNAP, which advised this set of commands:

sudo apt update
sudo apt install software-properties-common
sudo apt update

so I did that, but when I run sudo add-apt-repository ppa:deadsnakes/ppa I now get this error message:

~$ sudo add-apt-repository ppa:deadsnakes/ppa
Traceback (most recent call last):
  File "/usr/bin/add-apt-repository", line 12, in <module>
    from softwareproperties.SoftwareProperties import SoftwareProperties, shortcut_handler
  File "/usr/lib/python3/dist-packages/softwareproperties/SoftwareProperties.py", line 28, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

I have found some web links that show a wide variety of solutions with earlier versions of Python. I'm currently on Python 3.8.

Before I do anything more I want to ask what is the best way to solve the ModuleNotFoundError: No module named 'apt_pkg' error message when trying to install the deadsnakes repository to install Python 3.10, given the number of possible solutions I have seen.

Thanks very much.

Upvotes: 9

Views: 18035

Answers (3)

Robert Calhoun
Robert Calhoun

Reputation: 5153

No amount of fiddling with apt install --reinstall fixed this for me, which in my case is due to usr/lib/python3 not being included in apt-python's sys.path. (I presumably broke this by installing python from source.)

As a hack of last resort, I forced /usr/bin/apt-listchanges into finding apt_pkg.so by modifying it:

  cat /usr/bin/apt-listchanges
  (...)
  import sys, os, os.path
  import functools
+ sys.path.append("/usr/lib/python3/dist-packages")
  import apt_pkg
  (...)

This is gross, but I am only trying to fix apt well enough to uninstall everything and do a dist-upgrade.

Upvotes: 0

b0tting
b0tting

Reputation: 637

The issue is that the python_apt comes from the Ubuntu repo, offering python-apt for the default python 3.8, while you are running python 3.10. Copying the .so from Python 3.8 is not a clean solution. You are missing the expected apt_inst here causing recent Ansible libraries to fail. My approach was to recompile the python-apt library from python 3.10, this recreates the relevant python libraries:

git clone https://salsa.debian.org/apt-team/python-apt
cd python-apt && apt build-dep ./ -y && python3 setup.py build && python3 setup.py install

Upvotes: 1

Radjin
Radjin

Reputation: 356

This worked for me:

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

The 38 in the filename above can be different for you.

Upvotes: 34

Related Questions