Note: This guide also applies to python3.5 package problems after upgrading Debian 9 (Stretch) to 10 (Buster).
Update March 31st 2022: If you experience issues removing Python packages after a distribution upgrade inside a LXC container, check out this article: Unable to remove old Python packages in LXC container after Debian distribution upgrade.
In the past few weeks I've upgraded a lot of servers from Ubuntu 14.04 to 18.04, using 16.04 as intermediate version. See related articles "It is 2019, time to upgrade your Ubuntu 14.04 Trusty machines!" and "Updating Ubuntu with cross versions on LXC hosts and containers: Watch the gotchas!".
Today one machine got into troubles when the available disk space ran out during the last few packages of the Bionic upgrade. This left apt broken:
# apt-get upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt --fix-broken install' to correct these.
The following packages have unmet dependencies:
python3.5-minimal : Depends: libpython3.5-minimal (= 3.5.2-2ubuntu0~16.04.5) but it is not installable
Recommends: python3.5 but it is not installable
E: Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution).
Of course I tried apt --fix-broken install, as mentioned in the output, but this failed, too:
# apt --fix-broken install
Reading package lists... Done
Building dependency tree
Reading state information... Done
Correcting dependencies... Done
The following packages will be REMOVED:
python3.5-minimal
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 9,064 kB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 50653 files and directories currently installed.)
Removing python3.5-minimal (3.5.2-2ubuntu0~16.04.5) ...
Unlinking and removing bytecode for runtime python3.5
E: py3clean:65: cannot find magic tag for Python 3.5: python3.5 -c 'import imp; print(imp.get_tag())' failed with status code 134
update-binfmts: warning: unable to open /proc/sys/fs/binfmt_misc/python3.5 for writing: Permission denied
update-binfmts: warning: unable to disable binary format python3.5
update-binfmts: exiting due to previous errors
dpkg: error processing package python3.5-minimal (--remove):
installed python3.5-minimal package pre-removal script subprocess returned error exit status 2
Errors were encountered while processing:
python3.5-minimal
E: Sub-process /usr/bin/dpkg returned an error code (1)
I tried to manually remove python3.5-minimal, which failed as well:
# apt-get remove python3.5-minimal
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be REMOVED:
python3.5-minimal
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 9,064 kB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 50653 files and directories currently installed.)
Removing python3.5-minimal (3.5.2-2ubuntu0~16.04.5) ...
Unlinking and removing bytecode for runtime python3.5
E: py3clean:65: cannot find magic tag for Python 3.5: python3.5 -c 'import imp; print(imp.get_tag())' failed with status code 134
update-binfmts: warning: unable to open /proc/sys/fs/binfmt_misc/python3.5 for writing: Permission denied
update-binfmts: warning: unable to disable binary format python3.5
update-binfmts: exiting due to previous errors
dpkg: error processing package python3.5-minimal (--remove):
installed python3.5-minimal package pre-removal script subprocess returned error exit status 2
Errors were encountered while processing:
python3.5-minimal
E: Sub-process /usr/bin/dpkg returned an error code (1)
Why does it actually fail? I tried to manually run the python3.5 command and this finally shed some light into the problem:
# python3.5
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
Current thread 0x00007f7d3e217740 (most recent call first):
Aborted (core dumped)
I tried to re-install python3.5 but failed due to a missing dependency:
# apt-get install python3.5-minimal
Reading package lists... Done
Building dependency tree
Reading state information... Done
python3.5-minimal is already the newest version (3.5.2-2ubuntu0~16.04.5).
You might want to run 'apt --fix-broken install' to correct these.
The following packages have unmet dependencies:
python3.5-minimal : Depends: libpython3.5-minimal (= 3.5.2-2ubuntu0~16.04.5) but it is not installable
Recommends: python3.5 but it is not installable
E: Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution).
Checking the repositories, there is indeed no python3.5 package anymore in bionic:
# apt-cache search python3.5
python3.5-minimal - Minimal subset of the Python language (version 3.5)
The upgrade procedure from Xenial to Bionic was supposed to remove the python3.5 package (replaced by python3.6), but due to the full file system the upgrade aborted and left the system in a "in between" state.
Luckily the deb packages are still in the apt cache:
# ls -la /var/cache/apt/archives/python3.5*
-rw-r--r-- 1 root root 165264 Nov 13 16:28 /var/cache/apt/archives/python3.5_3.5.2-2ubuntu0~16.04.5_amd64.deb
-rw-r--r-- 1 root root 1598256 Nov 13 16:28 /var/cache/apt/archives/python3.5-minimal_3.5.2-2ubuntu0~16.04.5_amd64.deb
By using the information from an older article (How to fix a broken or half installed Debian deb package) I was able to force a re-installation from these package files:
# dpkg -i --force-overwrite python3.5_3.5.2-2ubuntu0~16.04.5_amd64.deb python3.5-minimal_3.5.2-2ubuntu0~16.04.5_amd64.deb
Selecting previously unselected package python3.5.
(Reading database ... 50655 files and directories currently installed.)
Preparing to unpack python3.5_3.5.2-2ubuntu0~16.04.5_amd64.deb ...
Unpacking python3.5 (3.5.2-2ubuntu0~16.04.5) ...
Selecting previously unselected package python3.5-minimal.
Preparing to unpack python3.5-minimal_3.5.2-2ubuntu0~16.04.5_amd64.deb ...
Unpacking python3.5-minimal (3.5.2-2ubuntu0~16.04.5) over (3.5.2-2ubuntu0~16.04.5) ...
dpkg: dependency problems prevent configuration of python3.5:
python3.5 depends on libpython3.5-stdlib (= 3.5.2-2ubuntu0~16.04.5); however:
Package libpython3.5-stdlib is not installed.
dpkg: error processing package python3.5 (--install):
dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of python3.5-minimal:
python3.5-minimal depends on libpython3.5-minimal (= 3.5.2-2ubuntu0~16.04.5); however:
Package libpython3.5-minimal:amd64 is not installed.
dpkg: error processing package python3.5-minimal (--install):
dependency problems - leaving unconfigured
Processing triggers for mime-support (3.60ubuntu1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Errors were encountered while processing:
python3.5
python3.5-minimal
Dang, almost! Two additional deb packages need to be installed as required dependencies, too:
# dpkg -i --force-overwrite python3.5_3.5.2-2ubuntu0~16.04.5_amd64.deb python3.5-minimal_3.5.2-2ubuntu0~16.04.5_amd64.deb libpython3.5-stdlib_3.5.2-2ubuntu0~16.04.5_amd64.deb libpython3.5-minimal_3.5.2-2ubuntu0~16.04.5_amd64.deb
(Reading database ... 51254 files and directories currently installed.)
Preparing to unpack python3.5_3.5.2-2ubuntu0~16.04.5_amd64.deb ...
Unpacking python3.5 (3.5.2-2ubuntu0~16.04.5) over (3.5.2-2ubuntu0~16.04.5) ...
Preparing to unpack python3.5-minimal_3.5.2-2ubuntu0~16.04.5_amd64.deb ...
Unpacking python3.5-minimal (3.5.2-2ubuntu0~16.04.5) over (3.5.2-2ubuntu0~16.04.5) ...
Preparing to unpack libpython3.5-stdlib_3.5.2-2ubuntu0~16.04.5_amd64.deb ...
Unpacking libpython3.5-stdlib:amd64 (3.5.2-2ubuntu0~16.04.5) over (3.5.2-2ubuntu0~16.04.5) ...
Selecting previously unselected package libpython3.5-minimal:amd64.
Preparing to unpack libpython3.5-minimal_3.5.2-2ubuntu0~16.04.5_amd64.deb ...
Unpacking libpython3.5-minimal:amd64 (3.5.2-2ubuntu0~16.04.5) ...
Setting up libpython3.5-minimal:amd64 (3.5.2-2ubuntu0~16.04.5) ...
Setting up python3.5-minimal (3.5.2-2ubuntu0~16.04.5) ...
update-binfmts: warning: unable to open /proc/sys/fs/binfmt_misc/python3.5 for writing: Permission denied
update-binfmts: warning: unable to disable binary format python3.5
Setting up libpython3.5-stdlib:amd64 (3.5.2-2ubuntu0~16.04.5) ...
Setting up python3.5 (3.5.2-2ubuntu0~16.04.5) ...
Processing triggers for mime-support (3.60ubuntu1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
This seems to have worked now. Let's try python3.5 again:
# python3.5
Python 3.5.2 (default, Nov 12 2018, 13:43:14)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
The manual re-installation worked. What about the status of apt now?
# apt-get upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
All good again!
No comments yet.
AWS Android Ansible Apache Apple Atlassian BSD Backup Bash Bluecoat CMS Chef Cloud Coding Consul Containers CouchDB DB DNS Database Databases Docker ELK Elasticsearch Filebeat FreeBSD Galera Git GlusterFS Grafana Graphics HAProxy HTML Hacks Hardware Icinga Influx Internet Java KVM Kibana Kodi Kubernetes LVM LXC Linux Logstash Mac Macintosh Mail MariaDB Minio MongoDB Monitoring Multimedia MySQL NFS Nagios Network Nginx OSSEC OTRS Office PGSQL PHP Perl Personal PostgreSQL Postgres PowerDNS Proxmox Proxy Python Rancher Rant Redis Roundcube SSL Samba Seafile Security Shell SmartOS Solaris Surveillance Systemd TLS Tomcat Ubuntu Unix VMWare VMware Varnish Virtualization Windows Wireless Wordpress Wyse ZFS Zoneminder