Why is apt so dependent on individual Python versions?

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?

Asked By: Esther

||

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 the control.tar.xz file, you will see a prerm script.

The script is an sh script but you can see that it also calls on python:

#!/bin/sh
set -e

# Automatically added by dh_python3:
if which py3clean >/dev/null 2>&1; then
    py3clean -p software-properties-gtk 
else
    dpkg -L software-properties-gtk | perl -ne 's,/([^/]*).py$,/__pycache__/1.*, or next; unlink $_ or die $! foreach glob($_)'
    find /usr/lib/python3/dist-packages/ -type d -name __pycache__ -empty -print0 | xargs --null --no-run-if-empty rmdir
fi

# End automatically added section

So py3clean is part of python3-minimal and this script calls on python3 in the shebang on the first line of the file as seen from the following commands:

which py3clean

and this should show /usr/bin/py3clean so:

head /usr/bin/py3clean 

output:

#! /usr/bin/python3
# vim: et ts=4 sw=4

# Copyright © 2010-2012 Piotr Ożarowski <piotr@debian.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is

This is important because an actual file for /usr/bin/python3 does not exist as you can see by the following command:

file /usr/bin/python3

On 20.04, this tells me that /usr/bin/python3 is a symbolic link to python3.8 and which 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 or python3.

So if you do install an alternate version you will not have the convenience of calling that alternate version by python or python3 as your default version — you will need to specify the version specifically by calling python3.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.

Answered By: mchid
Categories: Answers 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.