ModuleNotFoundError: No module named 'apt_pkg' installing deadsnakes repository

Question:

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.

Asked By: RTC222

||

Answers:

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.

Answered By: Radjin
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.