"Your python3 install is corrupted"

44,777

Solution 1

You need to use the default Python 3 version for 16.04. That's 3.5, not 3.6. So run:

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

If that doesn't work, try reinstalling the python3 package.

sudo apt-get install --reinstall python3

By the way, update-alternatives --display python3 should give you update-alternatives: error: no alternatives for python3. Different versions of Python are not alternatives in Ubuntu.

Solution 2

I just ran into this problem on Pop!_OS 18.04, trying to upgrade to 18.10, and it turns out that the problem lay in the symlink for /usr/bin/python and not for /usr/bin/python3. I had had /usr/bin/python3.6 configured as an alternative for python (not python3), and when I changed this, then I could run do-release-upgrade as expected.

I wish the error message pointed to python and not python3.


Before, with the problem:

$ update-alternatives --display python
python - manual mode
  link best version is /usr/bin/python3.6
  link currently points to /usr/bin/python2.7
  link python is /usr/bin/python
/usr/bin/python2.7 - priority 1
/usr/bin/python3.6 - priority 2 

I fixed it this way:

$ sudo update-alternatives --remove-all python
$ sudo ln -sf /usr/bin/python2.7 /usr/bin/python

Also see this comment below which describes a more precise solution that also better explains what is going on and how to fix it.

Solution 3

None of the answers here seem to explain how you can get to the solution yourself, so I took on a journey, in my case inspecting do-release-upgrade in KDE Neon on Ubuntu 18 LTS.

First, I ran it with tracefile -w and discovered that the actual release-upgrade-scripts where downloaded into a /tmp/ubuntu-release-upgrader-xxxxxxxx directory.

Using grep in that directory, I found the error message in DistUpgradeController.py:

❯ grep --line-number --recursive --binary-files=without-match "python3 install is corrupted"
DistUpgradeController.py:426:                             _("Your python3 install is corrupted. "

So I inspected the surrounding code, which used the function _pythonSymlinkCheck, jumped to that and discovered the root of the problem: The script expected the symlink /usr/bin/python3 to resolve to exactly /usr/bin/<debian_default_python>:

binaries_and_dirnames = [("python3", "python3")]
for binary, dirname in binaries_and_dirnames:
    debian_defaults = '/usr/share/%s/debian_defaults' % dirname
    if os.path.exists(debian_defaults):
        config = SafeConfigParser()
        with open(debian_defaults) as f:
            config.readfp(f)
        try:
            expected_default = config.get('DEFAULT', 'default-version')
        except NoOptionError:
            logging.debug("no default version for %s found in '%s'" %
                          (binary, config))
            return False
        try:
            fs_default_version = os.readlink('/usr/bin/%s' % binary)
        except OSError as e:
            logging.error("os.readlink failed (%s)" % e)
            return False
        if not fs_default_version in (expected_default, os.path.join('/usr/bin', expected_default)):

As visible from the script, <debian_default_python> is the default-version key in the DEFAULT section from /usr/share/python3/debian_defaults:

❯ cat /usr/share/python3/debian_defaults
[DEFAULT]
# the default python3 version
default-version = python3.6

My link did point to /usr/bin/python3.6, but via an extra indirection from update-alternatives, which the script doesn't resolve:

❯ python
Python 3.6.9 (default, Jul 17 2020, 12:50:27)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.readlink("/usr/bin/python3")
'/etc/alternatives/python3'
>>> os.readlink("/etc/alternatives/python3")
'/usr/bin/python3.6'

So in the end I also resolved to the nuclear option, but now with full knowledge of what was going on :)

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

Solution 4

I observed this error message on Windows 10 1903 running WSL Ubuntu when I wanted to upgrade from 16.04 LTS to 18.04 LTS.

After do-release-upgrade had failed, I switched python alternatives to every choice offered by update-alternatives --config python and ran the upgrade command again. That did not help.

Then I checked the log file /var/log/dist-upgrade/main.log which contained the lines

2019-09-02 20:58:08,686 DEBUG _pythonSymlinkCheck run
2019-09-02 20:58:08,687 DEBUG python symlink points to: '/etc/alternatives/python', but expected is 'python2.7' or
'/usr/bin/python2.7'
2019-09-02 20:58:08,688 ERROR pythonSymlinkCheck() failed, aborting

So although the error message mentions python3, the issue is about python2.

The upgrade script checks for /usr/bin/python linking to /usr/bin/python2, see the source code of DistUpgrade/DistUpgradeController.py here: ubuntu launchpad

So one solution is to completely remove python from the alternative system and add the link manually, as described in the most popular answer.

If you don't want to remove python from the alternative system, just change the link only for the time during the upgrade process:

# rm /usr/bin/python 
# ln -sf /usr/bin/python2.7 /usr/bin/python
# do-release-upgrade

This worked for me.

During the upgrade process, the link is automatically repaired. So when the upgrade is finished, it points to the python entry in the alternatives directory:

$ ls -l /usr/bin/python
lrwxrwxrwx 1 root root 24 Sep  2 22:01 /usr/bin/python -> /etc/alternatives/python

Edit: for thorough information, the issue might also appear if you upgrade from 18.04 LTS to 19.04 and the anwser applies to this situation, too.

Solution 5

Basically the solution to this problem consists of making /usr/bin/python point to the right version of Python your Ubuntu release expects (for instance, in 16.04 was Python2.7 and in 18.04 was Python3.6).

If you have several versions of Python installed in your system, you might be using update-alternatives to manage them. It doesn't matter much your default alternative for Python is the right version your system expects (3.6 in Ubuntu 18.04), it won't work.

The reason why this doesn't work is that, when using update-alternatives, /usr/bin/python3 points to /etc/alternatives/python3, and it seems that's not exactly the same as making /usr/bin/python3 point to /usr/bin/python3.6.

That's why the solution to this problem often consists of stop managing your Python3 versions with update-alternatives and make /usr/bin/python3 point to the right version of Python3 your system expects.

Share:
44,777

Related videos on Youtube

mRcSchwering
Author by

mRcSchwering

Updated on September 18, 2022

Comments

  • mRcSchwering
    mRcSchwering over 1 year

    I want to upgrade from Ubuntu 16.04.5 LTS to 18.04, so ran sudo do-release-upgrade. After downloading and extracting bionic.tar.gz I get:

    Can not upgrade 
    
    Your python3 install is corrupted. Please fix the '/usr/bin/python3'
    symlink.
    

    I saw How to fix "python installation is corrupted"? and so I did sudo ln -sf /usr/bin/python3.6 /usr/bin/python3 thinking that it would be a similar problem. But that didn't work (still same error message).

    I have a few python versions:

    $ ls /usr/lib | grep python
    python2.7
    python3
    python3.5
    python3.6
    
    $ update-alternatives --display python3
    python3 - auto mode
      link best version is /usr/bin/python3.6
      link currently points to /usr/bin/python3.6
      link python3 is /usr/bin/python3
    /usr/bin/python3.5 - priority 1
    /usr/bin/python3.6 - priority 2
    

    How do I fix python3?

    • Kulfy
      Kulfy over 5 years
      And what about reinstallation (as mentioned in accepted answer)?
  • Sumit Jain
    Sumit Jain about 5 years
    Yes, I can confirm that the solution works, this should be accepted answer.
  • KiriSakow
    KiriSakow almost 5 years
    Opting for update-alternatives --remove-all python was definitely an unnecessary overkill: All you needed to do was using update-alternatives --config python to have python point to the latest python2.* (e.g. python2.7), then use update-alternatives --config python3 to have python3 point to specifically python3.6 — which is the default Python 3 version for 18.04.
  • KiriSakow
    KiriSakow almost 5 years
    If you need to upgrade to python 3.7 in Ubuntu 18.04, don't do it systemwide — or you're bound to end up having nasty little problems systemwide with essential tools like gnome-terminal, update-manager, etc. Rather use virtual environments (documentation here and here)
  • wjandrea
    wjandrea almost 5 years
    @Kiri There are ways to install other versions of Python without replacing the system one(s). For example using the deadsnakes PPA.
  • Daniel K.
    Daniel K. over 4 years
    @KiriSakow: update-alternatives --config python3 does not work as python3 is not a valid "link group" in the alternatives system.
  • Daniel K.
    Daniel K. over 4 years
    Note: This answer also applies to an upgrade from Ubuntu 18.04 LTS to 19.04. I tried it myself after the update to 18.04 had finished successfully.
  • LnxSlck
    LnxSlck over 4 years
    This is what worked for me. I had both python and python3 poiting to python3.6. I had to point /usr/bin/python back to python2.6
  • wjandrea
    wjandrea over 4 years
    /usr/bin/python doesn't exist on a clean install of 18.04, but it does if you do an upgrade instead of a clean install or install the python package, in which case it should be Python 2.7, not 3.6. See PEP 394.
  • wjandrea
    wjandrea over 4 years
    To be clear, different versions of Python are not alternatives on Ubuntu and should not be managed with update-alternatives. This is because the OS relies on a certain version being installed.
  • Palo
    Palo about 4 years
    Unfortunately, it does not work. kempelen.dai.fmph.uniba.sk/python_headache.png The system is up to date, and I have reinstalled the python and python3 packages, still the same. It is unique to this machine, other similar machine upgraded fine.
  • wjandrea
    wjandrea about 4 years
    @palo I'm thinking that the reinstall command I mentioned previously might not actually reinstall everything. Try sudo apt-get install --reinstall python3 python3-minimal python3.5 python3.5-minimal.
  • Palo
    Palo about 4 years
    thank you for a quick response. I tried as you suggested, but the same message "Your python3 install is corrupted. Please fix the '/usr/bin/python3' symlink" is still in the way. Uninstalling python3 is not much an option as it blows away too many dependent packages. If you have any idea what to investigate, I will do that.
  • Palo
    Palo about 4 years
    It could have something to do with software installed by my colleagues on this machine in some local folders. I see there is linux embedded for rpi, miniconda3 that includes python3.6, and /usr/local/python2.7. Still which python reports /usr/bin/python, but pip goes from /usr/local/bin/pip. Even after changing /etc/environment placing /usr/local/... at the very end of the PATH, still the same message. I noticed that pyversions -d reports: "/usr/bin/python does not match the python default version. It must be reset to point to python2.7". So I replaced the link /usr/bin/python to point...
  • Palo
    Palo about 4 years
    ...directly to /usr/bin/python2.7 instead of /etc/alternatives/python, and suddenly pyversions -d was happy, and do-release-upgrade did not complain about python and started the upgrade process as expected! So the problem was probably only this indirect double-linked path from python to python2.7 through /etc/alternatives.
  • wjandrea
    wjandrea about 4 years
    @Palo Ah so you found the same solution as the top-voted answer
  • Palo
    Palo about 4 years
    Well, I found that answer very confusing. First, I have no idea what "Pop!_OS 18.04" is, and I was upgrading from LTS 16.04 to 18.04, not from 18.04 to 18.10, and also, I have no idea what possible consequences could sudo update-alternatives --remove-all python have, so I did not bother doing that and rather tried a small step that I can easily undo. But good that you point that. :) Thanks.
  • Palo
    Palo about 4 years
    And it would be nice to fix that upgrade script eventually to 1) not report misinformation about python3 link, and 2) detect this automatically not to stay in the way of poor users who like to bother with python about as much as waking up after 30 minutes long night. I mean, if somebody invents a language where the amount of white-space changes semantics, that guy must have never really used a computer.
  • jcadam
    jcadam over 3 years
    This is the right answer only. None above actually pointed out where the root cause was.
  • mmv_sat
    mmv_sat over 3 years
    my /usr/bin/python was pointing to python 3.7 instead of 2.7, and my /usr/bin/python3 was also pointing to 3.7. getting /usr/bin/python pointed to 2.7 cleared my python3 corrupt error
  • FKEinternet
    FKEinternet over 3 years
    It would be nice if the error message reported what the actual error is - python not being set correctly instead of python3 ...
  • Chan Kim
    Chan Kim about 3 years
    I also solved this by sudo ln -s python2.7 python; sudo ln -s python3.5 python3. It looks like do-release-upgrade doesn't recognize alternatives settings. python and python3 should directly point to python2.7 and python3.5 (for ubuntu 16.04)
  • michaeljtbrooks
    michaeljtbrooks over 2 years
    This worked for me. The symlinks in /usr/bin/python and /usr/bin/python3 must be absolute: tail /var/log/dist-upgrade/main.log revealed 2021-10-07 14:34:42,156 DEBUG python symlink points to: './python2.7', but expected is 'python2.7' or '/usr/bin/python2.7'. You need all three python links intact and absolute: /usr/bin/python /usr/bin/python2 /usr/bin/python3.
  • Pradip Vaghasiya
    Pradip Vaghasiya over 2 years
    My goodness, error message could have been better. Thousands of hours must have been wasted for all. It is not a problem of corruption, it is differnet version. People would only think of reinstalling by that word.
  • cmbarbu
    cmbarbu over 2 years
    This worked for me to update from ubuntu 20.04.02 to 20.04.03 except that it was sudo ln -sf /usr/bin/python3.6 /usr/bin/python3
  • Ben Kovitz
    Ben Kovitz over 2 years
    This answer is excellent. It not only solves the problem, it shows how to figure out how to solve the problem.