3

I recently installed python 3.7 on a Ubuntu 18.04.4 LTS machine using the following command:

sudo apt-get install python3.7

I also ran the following command since I want python 3.7 to be run as python3:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1

Subsequently I followed a suggestion to run this:

sudo apt autoremove

And now when I try to enable the universe repository (add-apt-repository universe), I get the following error:

Traceback (most recent call last):
  File "/usr/bin/add-apt-repository", line 11, 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'

Most of the answers I find tell me to re-install python3-apt like here and here, but that is not working.

What else can I try?

EDIT:

Here and here are also suggestions that involve specifically pointing to the dist installation of python, which is version 3.6 for Ubuntu 18.04. Is it safe to do this? Couldn't this python be upgraded at some point?

2
  • 1
    Is it safe to do this?: Yes. Couldn't this python be upgraded at some point?: Applications in Ubuntu depends on the distribution default python version. They may or may not be compatible with higher/lower versions. If you really need Python 3.7, either invoke it using python3.7 or edit shebang of the source code (if using scripts). Changing Python versions can sometime break the OS. I'd recommend you to rollback the changes and make python3 a symlink of python3.6.
    – Kulfy
    Apr 21 '20 at 16:41
  • After some circling I actually ended up coming to this conclusion, just added an answer below a couple of minutes ago. Very good to get some more information that confirms this, thank you!
    – Nanna
    Apr 21 '20 at 16:49
8

As it turns out, my solution was not exactly to fix why the apt-pkg module could not be found, but rather to understand the dos and don'ts of installing another version of python to Ubuntu than the distro one.

I ended up removing the alternative I had added to point python3 to python3.7

sudo update-alternatives --remove-all python3

and instead fixing the symlink to point at the original 3.6 again, as distributed with Ubuntu 18.04

sudo ln -sf /usr/bin/python3.6 /usr/bin/python3

Furthermore, now I will use the command python3.7 for everything I need Python 3.7 for, which is basically just creating my virtualenv so it's not that much of a hazzle.

Lesson learned!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.