Can't use apt-get on Ubuntu 16.04 VM cuz missing python3

797 Views Asked by At

I can't seem to find a solution for this. I need sudo apt-get install <package> to work because sudo apt-get -f -qq install -y doesn't work.

Here is the log:

ubuntu@ip-10-8-0-145:~$ sudo apt-get -f -qq install -y
Preconfiguring packages ...
(Reading database ... 104324 files and directories currently installed.)
Preparing to unpack .../dh-python_2.20151103ubuntu1.2_all.deb ...
/var/lib/dpkg/info/dh-python.prerm: 6: /var/lib/dpkg/info/dh-python.prerm: py3clean: not found
dpkg: warning: subprocess old pre-removal script returned error exit status 127
dpkg: trying script from the new package instead ...
/var/lib/dpkg/tmp.ci/prerm: 6: /var/lib/dpkg/tmp.ci/prerm: py3clean: not found
dpkg: error processing archive /var/cache/apt/archives/dh-python_2.20151103ubuntu1.2_all.deb (--unpack):
 subprocess new pre-removal script returned error exit status 127
/var/lib/dpkg/info/dh-python.postinst: 6: /var/lib/dpkg/info/dh-python.postinst: py3compile: not found
dpkg: error while cleaning up:
 subprocess installed post-installation script returned error exit status 127
Preparing to unpack .../python3-distupgrade_1%3a16.04.32_all.deb ...
/var/lib/dpkg/info/python3-distupgrade.prerm: 6: /var/lib/dpkg/info/python3-distupgrade.prerm: py3clean: not found
dpkg: warning: subprocess old pre-removal script returned error exit status 127
dpkg: trying script from the new package instead ...
/var/lib/dpkg/tmp.ci/prerm: 6: /var/lib/dpkg/tmp.ci/prerm: py3clean: not found
dpkg: error processing archive /var/cache/apt/archives/python3-distupgrade_1%3a16.04.32_all.deb (--unpack):
 subprocess new pre-removal script returned error exit status 127
/var/lib/dpkg/info/python3-distupgrade.postinst: 6: /var/lib/dpkg/info/python3-distupgrade.postinst: py3compile: not found
dpkg: error while cleaning up:
 subprocess installed post-installation script returned error exit status 127
Preparing to unpack .../python3-update-manager_1%3a16.04.17_all.deb ...
/var/lib/dpkg/info/python3-update-manager.prerm: 6: /var/lib/dpkg/info/python3-update-manager.prerm: py3clean: not found
dpkg: warning: subprocess old pre-removal script returned error exit status 127
dpkg: trying script from the new package instead ...
/var/lib/dpkg/tmp.ci/prerm: 6: /var/lib/dpkg/tmp.ci/prerm: py3clean: not found
dpkg: error processing archive /var/cache/apt/archives/python3-update-manager_1%3a16.04.17_all.deb (--unpack):
 subprocess new pre-removal script returned error exit status 127
/var/lib/dpkg/info/python3-update-manager.postinst: 6: /var/lib/dpkg/info/python3-update-manager.postinst: py3compile: not found
dpkg: error while cleaning up:
 subprocess installed post-installation script returned error exit status 127
Preparing to unpack .../python3-problem-report_2.20.1-0ubuntu2.30_all.deb ...
/var/lib/dpkg/info/python3-problem-report.prerm: 6: /var/lib/dpkg/info/python3-problem-report.prerm: py3clean: not found
dpkg: warning: subprocess old pre-removal script returned error exit status 127
dpkg: trying script from the new package instead ...
/var/lib/dpkg/tmp.ci/prerm: 6: /var/lib/dpkg/tmp.ci/prerm: py3clean: not found
dpkg: error processing archive /var/cache/apt/archives/python3-problem-report_2.20.1-0ubuntu2.30_all.deb (--unpack):
 subprocess new pre-removal script returned error exit status 127
No apport report written because MaxReports is reached already
                                                              /var/lib/dpkg/info/python3-problem-report.postinst: 6: /var/lib/dpkg/info/python3-problem-report.postinst: py3compile: not found
dpkg: error while cleaning up:
 subprocess installed post-installation script returned error exit status 127
Preparing to unpack .../python3-apport_2.20.1-0ubuntu2.30_all.deb ...
/var/lib/dpkg/info/python3-apport.prerm: 6: /var/lib/dpkg/info/python3-apport.prerm: py3clean: not found
dpkg: warning: subprocess old pre-removal script returned error exit status 127
dpkg: trying script from the new package instead ...
/var/lib/dpkg/tmp.ci/prerm: 6: /var/lib/dpkg/tmp.ci/prerm: py3clean: not found
dpkg: error processing archive /var/cache/apt/archives/python3-apport_2.20.1-0ubuntu2.30_all.deb (--unpack):
 subprocess new pre-removal script returned error exit status 127
No apport report written because MaxReports is reached already
                                                              /var/lib/dpkg/info/python3-apport.postinst: 6: /var/lib/dpkg/info/python3-apport.postinst: py3compile: not found
dpkg: error while cleaning up:
 subprocess installed post-installation script returned error exit status 127
Preparing to unpack .../python3-cryptography_1.2.3-1ubuntu0.3_amd64.deb ...
/var/lib/dpkg/info/python3-cryptography.prerm: 6: /var/lib/dpkg/info/python3-cryptography.prerm: py3clean: not found
dpkg: warning: subprocess old pre-removal script returned error exit status 127
dpkg: trying script from the new package instead ...
/var/lib/dpkg/tmp.ci/prerm: 6: /var/lib/dpkg/tmp.ci/prerm: py3clean: not found
dpkg: error processing archive /var/cache/apt/archives/python3-cryptography_1.2.3-1ubuntu0.3_amd64.deb (--unpack):
 subprocess new pre-removal script returned error exit status 127
No apport report written because MaxReports is reached already
                                                              /var/lib/dpkg/info/python3-cryptography.postinst: 6: /var/lib/dpkg/info/python3-cryptography.postinst: py3compile: not found
dpkg: error while cleaning up:
 subprocess installed post-installation script returned error exit status 127
Preparing to unpack .../python3-urllib3_1.13.1-2ubuntu0.16.04.4_all.deb ...
/var/lib/dpkg/info/python3-urllib3.prerm: 6: /var/lib/dpkg/info/python3-urllib3.prerm: py3clean: not found
dpkg: warning: subprocess old pre-removal script returned error exit status 127
dpkg: trying script from the new package instead ...
/var/lib/dpkg/tmp.ci/prerm: 6: /var/lib/dpkg/tmp.ci/prerm: py3clean: not found
dpkg: error processing archive /var/cache/apt/archives/python3-urllib3_1.13.1-2ubuntu0.16.04.4_all.deb (--unpack):
 subprocess new pre-removal script returned error exit status 127
No apport report written because MaxReports is reached already
                                                              /var/lib/dpkg/info/python3-urllib3.postinst: 6: /var/lib/dpkg/info/python3-urllib3.postinst: py3compile: not found
dpkg: error while cleaning up:
 subprocess installed post-installation script returned error exit status 127
Preparing to unpack .../software-properties-common_0.96.20.10_all.deb ...
/var/lib/dpkg/info/software-properties-common.prerm: 6: /var/lib/dpkg/info/software-properties-common.prerm: py3clean: not found
dpkg: warning: subprocess old pre-removal script returned error exit status 127
dpkg: trying script from the new package instead ...
/var/lib/dpkg/tmp.ci/prerm: 6: /var/lib/dpkg/tmp.ci/prerm: py3clean: not found
dpkg: error processing archive /var/cache/apt/archives/software-properties-common_0.96.20.10_all.deb (--unpack):
 subprocess new pre-removal script returned error exit status 127
No apport report written because MaxReports is reached already
                                                              /var/lib/dpkg/info/software-properties-common.postinst: 6: /var/lib/dpkg/info/software-properties-common.postinst: py3compile: not found
dpkg: error while cleaning up:
 subprocess installed post-installation script returned error exit status 127
Preparing to unpack .../python3-software-properties_0.96.20.10_all.deb ...
/var/lib/dpkg/info/python3-software-properties.prerm: 6: /var/lib/dpkg/info/python3-software-properties.prerm: py3clean: not found
dpkg: warning: subprocess old pre-removal script returned error exit status 127
dpkg: trying script from the new package instead ...
/var/lib/dpkg/tmp.ci/prerm: 6: /var/lib/dpkg/tmp.ci/prerm: py3clean: not found
dpkg: error processing archive /var/cache/apt/archives/python3-software-properties_0.96.20.10_all.deb (--unpack):
 subprocess new pre-removal script returned error exit status 127
No apport report written because MaxReports is reached already
                                                              /var/lib/dpkg/info/python3-software-properties.postinst: 6: /var/lib/dpkg/info/python3-software-properties.postinst: py3compile: not found
dpkg: error while cleaning up:
 subprocess installed post-installation script returned error exit status 127
Preparing to unpack .../sosreport_3.9.1-1ubuntu0.16.04.2_amd64.deb ...
/var/lib/dpkg/info/sosreport.prerm: 6: /var/lib/dpkg/info/sosreport.prerm: py3clean: not found
dpkg: warning: subprocess old pre-removal script returned error exit status 127
dpkg: trying script from the new package instead ...
/var/lib/dpkg/tmp.ci/prerm: 6: /var/lib/dpkg/tmp.ci/prerm: py3clean: not found
dpkg: error processing archive /var/cache/apt/archives/sosreport_3.9.1-1ubuntu0.16.04.2_amd64.deb (--unpack):
 subprocess new pre-removal script returned error exit status 127
No apport report written because MaxReports is reached already
                                                              /var/lib/dpkg/info/sosreport.postinst: 6: /var/lib/dpkg/info/sosreport.postinst: py3compile: not found
dpkg: error while cleaning up:
 subprocess installed post-installation script returned error exit status 127
Preparing to unpack .../cloud-init_21.1-19-gbad84ad4-0ubuntu1~16.04.2_all.deb ...
/var/lib/dpkg/info/cloud-init.prerm: 14: /var/lib/dpkg/info/cloud-init.prerm: py3clean: not found
dpkg: warning: subprocess old pre-removal script returned error exit status 127
dpkg: trying script from the new package instead ...
/var/lib/dpkg/tmp.ci/prerm: 14: /var/lib/dpkg/tmp.ci/prerm: py3clean: not found
dpkg: error processing archive /var/cache/apt/archives/cloud-init_21.1-19-gbad84ad4-0ubuntu1~16.04.2_all.deb (--unpack):
 subprocess new pre-removal script returned error exit status 127
No apport report written because MaxReports is reached already
                                                              /var/lib/dpkg/info/cloud-init.postinst: 329: /var/lib/dpkg/info/cloud-init.postinst: py3compile: not found
dpkg: error while cleaning up:
 subprocess installed post-installation script returned error exit status 127
Errors were encountered while processing:
 /var/cache/apt/archives/dh-python_2.20151103ubuntu1.2_all.deb
 /var/cache/apt/archives/python3-distupgrade_1%3a16.04.32_all.deb
 /var/cache/apt/archives/python3-update-manager_1%3a16.04.17_all.deb
 /var/cache/apt/archives/python3-problem-report_2.20.1-0ubuntu2.30_all.deb
 /var/cache/apt/archives/python3-apport_2.20.1-0ubuntu2.30_all.deb
 /var/cache/apt/archives/python3-cryptography_1.2.3-1ubuntu0.3_amd64.deb
 /var/cache/apt/archives/python3-urllib3_1.13.1-2ubuntu0.16.04.4_all.deb
 /var/cache/apt/archives/software-properties-common_0.96.20.10_all.deb
 /var/cache/apt/archives/python3-software-properties_0.96.20.10_all.deb
 /var/cache/apt/archives/sosreport_3.9.1-1ubuntu0.16.04.2_amd64.deb
 /var/cache/apt/archives/cloud-init_21.1-19-gbad84ad4-0ubuntu1~16.04.2_all.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)
ubuntu@ip-10-8-0-145:~$ 

From the log, I gather that python3 isn't installed. However, running sudo apt-get install python3 seems to indicate it is installed. Here is the log:

ubuntu@ip-10-8-0-145:~$ sudo apt-get install python3
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3 is already the newest version (3.5.1-3).
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
 ubuntu-release-upgrader-core : Depends: python3-distupgrade (= 1:16.04.32) but 1:16.04.25 is to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).
ubuntu@ip-10-8-0-145:~$ 

However, after checking /usr/bin/, I got no python3 binaries installed.

ubuntu@ip-10-8-0-145:~$ ls -aslt /usr/bin | grep -i python
    0 lrwxrwxrwx  1 root   root          23 Mar  1  2021 pdb2.7 -> ../lib/python2.7/pdb.py
    0 lrwxrwxrwx  1 root   root          33 Mar  1  2021 python2.7-config -> x86_64-linux-gnu-python2.7-config
 3412 -rwxr-xr-x  1 root   root     3492624 Mar  1  2021 python2.7
    4 -rwxr-xr-x  1 root   root        2909 Mar  1  2021 x86_64-linux-gnu-python2.7-config
    0 lrwxrwxrwx  1 root   root          23 Jan 26  2021 pdb3.5 -> ../lib/python3.5/pdb.py
    0 lrwxrwxrwx  1 root   root           9 Nov 24  2017 python -> python2.7
    0 lrwxrwxrwx  1 root   root           9 Nov 24  2017 python2 -> python2.7
    0 lrwxrwxrwx  1 root   root          16 Nov 24  2017 python2-config -> python2.7-config
    0 lrwxrwxrwx  1 root   root          16 Nov 24  2017 python-config -> python2.7-config
    0 lrwxrwxrwx  1 root   root          29 Nov 24  2017 pyversions -> ../share/python/pyversions.py
    0 lrwxrwxrwx  1 root   root          33 Nov 24  2017 x86_64-linux-gnu-python-config -> x86_64-linux-gnu-python2.7-config
    4 -rwxr-xr-x  1 root   root        1056 Nov 24  2017 dh_python2
    0 lrwxrwxrwx  1 root   root          26 May 18  2016 dh_pypy -> ../share/dh-python/dh_pypy
    0 lrwxrwxrwx  1 root   root          29 May 18  2016 dh_python3 -> ../share/dh-python/dh_python3
    0 lrwxrwxrwx  1 root   root          26 May 18  2016 pybuild -> ../share/dh-python/pybuild
    0 lrwxrwxrwx  1 root   root          31 Mar 23  2016 py3versions -> ../share/python3/py3versions.py
    4 -rwxr-xr-x  1 root   root         976 Nov 27  2015 python3-jsondiff
    4 -rwxr-xr-x  1 root   root        3662 Nov 27  2015 python3-jsonpatch
    4 -rwxr-xr-x  1 root   root         974 Nov 27  2015 python2-jsondiff
    4 -rwxr-xr-x  1 root   root        3660 Nov 27  2015 python2-jsonpatch
    4 -rwxr-xr-x  1 root   root        1342 Oct 24  2015 python3-jsonpointer
    4 -rwxr-xr-x  1 root   root        1340 Oct 24  2015 python2-jsonpointer
ubuntu@ip-10-8-0-145:~$ 

I tried to do what was instructed and run sudo apt-get install --reinstall python3 but got:

ubuntu@ip-10-8-0-145:~$ sudo apt-get install --reinstall python3
Reading package lists... Done
Building dependency tree       
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
 ubuntu-release-upgrader-core : Depends: python3-distupgrade (= 1:16.04.32) but 1:16.04.25 is to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).
ubuntu@ip-10-8-0-145:~$ 

Now, I have tried to follow numerous articles claiming to install python3 on ubuntu 16.04 without sudo apt-get command but can't seem to get it right. Can someone post instructions on how to fix my machine?

2

There are 2 best solutions below

0
ProgramMasher On

It may be because it is installed but it's not on the path so Ubuntu can't find it. If it says it's installed try using the which command to find it.

which python3

Then check to see if that is on your path

echo $PATH

If not add it to the path using

export PATH=/home/dave/work:$PATH

/home/dave/work will be your Python path

Edit: it may be as simple as running sudo apt-get -f install as suggested.

0
Pik Master On

I had the similar problem when upgrading Ubuntu 16 in EC2 virtual machine, because of the package sosreport - your issue might be a mutation of mine, so I hope this helps.

The symptoms:

$ sudo apt upgrade

Reading package lists... Done

...

Setting up sosreport (4.4-1ubuntu0.18.04.1) ...
  File "/usr/lib/python3/dist-packages/sos/report/plugins/gcp.py", line 99
    f"Failed to communicate with Metadata Server "
                                                 ^
SyntaxError: invalid syntax

dpkg: error processing package sosreport (--configure):
 installed sosreport package post-installation script subprocess returned error exit status 1
Errors were encountered while processing:
 sosreport
E: Sub-process /usr/bin/dpkg returned an error code (1)

Troubleshooting

It seems that the package fails in postinstall is because of py3compile

$ cat /var/lib/dpkg/info/sosreport.postinst 

#!/bin/sh
set -e

# Automatically added by dh_python3:
if which py3compile >/dev/null 2>&1; then
        py3compile -p sosreport 
fi

We can see that py3compile command has the problem with syntax:

$ py3compile -p sosreport 
  File "/usr/lib/python3/dist-packages/sos/report/plugins/gcp.py", line 99
    f"Failed to communicate with Metadata Server "
                                                 ^
SyntaxError: invalid syntax

Let's see which package contains it:

$ cd /var/lib/dpkg/info && grep py3compile *list

dh-python.list:/usr/share/debhelper/autoscripts/postinst-py3compile
python3-minimal.list:/usr/share/man/man1/py3compile.1.gz
python3-minimal.list:/usr/bin/py3compile

Solution

so just upgrade the python package:

$ sudo apt install python3-minimal

And then it compiles without errors:

$ py3compile -p sosreport 

So you can continue upgrading the system:

sudo apt upgrade