We get a lot of questions here about people messing with Python versions and ruining their systems, most noticeably that apt fails to function properly. I am aware that many Ubuntu packages require specific versions of Python to function properly.
My question is: what makes a newer/different Python version incompatible with apt? Do these packages depend on language features that only exist in certain versions, or what version-specific Python features does apt depend on that makes it only work with one particular Python version?
Yes, it usually is specific language features that only exist in a certain Python version.
Depending on the issue, you will often get an error when an application like apt or more likely dpkg calls for something using the wrong syntax that doesn't exist in the newer version.
However apt itself does not depend on python and the same is true for dpkg — but the packages that do depend on python often have pre and post install scripts that invoke python or python scripts.
For example, if you download the deb file for
software-properties-gtk
, extract that file, and then extract thecontrol.tar.xz
file, you will see aprerm
script.The script is an sh script but you can see that it also calls on python:
So
py3clean
is part ofpython3-minimal
and this script calls onpython3
in the shebang on the first line of the file as seen from the following commands:and this should show
/usr/bin/py3clean
so:output:
This is important because an actual file for
/usr/bin/python3
does not exist as you can see by the following command:On 20.04, this tells me that
/usr/bin/python3
is a symbolic link to python3.8 andwhich python3.8
shows this file is/usr/bin/python3.8
. Furthermore,file /usr/bin/python3.8
confirms that this is the actual Python executable used for python3.So the
software-properties-gtk
package is expecting that, if python3 is installed, that the version of python3 installed is python3.8.The
py3clean
script may or may not work with a different python version, but there's often enough changes to python that there's probably at least one change somewhere in that script that would cause some type of error.Even if not for this particular situation, you get the idea.
I guess to summarize, this isn't actually a problem with apt but more of a python problem because python scripts often use a generic shebang that expects a particular version of python when the shebang specifies
/usr/bin/python
or/usr/bin/python3
or even/usr/bin/env python
or/usr/bin/env python3
as none of these point to an actual file, they all point to a symbolic link that points to your default version of python installed on the system.Since most packages are released for a particular version of Ubuntu, they all expect the default version of Python to be the same. Otherwise, each package would depend on a arbitrarily different Python version and we may end up with 3 or more versions of Python installed on the same system just to satisfy dependencies.
Typically, if you want or need a different version of Python installed, it's because you have a particular reason. And there's nothing stopping you from using a different version of Python for that reason, so long as you do not mess with the default version of python that other software use when they call on
python
orpython3
.So if you do install an alternate version you will not have the convenience of calling that alternate version by
python
orpython3
as your default version — you will need to specify the version specifically by callingpython3.9
, for example, when you use it.I guess I should add as a disclaimer for others reading this answer. If you want to install a different version of python on your system, you should ask around to find the proper way to do it. It is possible to have multiple versions of Python on one system but to avoid problems, you must not uninstall, change, or alter in any way the default version of Python. But I don't think going into more detail would be within the scope of this question.