Error when trying sudo apt-get update - Python related?

Solution 1:

It is, as suggested by @guiverc, most likely Python version related. It seems that many Python tutorials these days suggests to change the default Python version from 2 to 3. While this is nice and practical for Python development, it breaks the packages which are using Python 2 scripts in there installation process.

So check where the link /usr/bin/python is pointing to:

$ ls -la /usr/bin/python
lrwxrwxrwx 1 root root 9 Jan 24  2017 /usr/bin/python -> python2.7

It should point to python2, not to any python3 executable. If it points to python3 then do the following (man ln):

$ sudo rm -f /usr/bin/python
$ sudo ln -s /usr/bin/python2.7 /usr/bin/python

After that, the apt-get will start to work again.

Background on Python interpreter version

Many scripts use the Shebang to control which interpreter is used to executed the following script. In most Python 2 scripts the following lines are used:

#!/usr/bin/env python

For Python 3 this shebang is used:

#!/usr/bin/env python3

If the default link to the Python 2 (/usr/bin/python -> python2.7) interpreter is changed to any version of Python 3, all "old" Python 2 scripts will stop working.

Python version and Ubuntu version

This answer is from 2018... and therefore it applies for Ubuntu 18.04 and probably older versions. For Ubuntu 20.04 the python v2 is more or less deprecated and it must be installed with the meta package sudo apt install python-is-python2. Also the /usr/bin/python link does not exists if only python v3 is installed.

So it is save to say: This answer is not valid for python3 and Ubuntu versions greater then 18.04.