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/", 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.


  • This worked for me:

    sudo apt-get install python3-apt --reinstall
    cd /usr/lib/python3/dist-packages
    sudo cp

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