无法在 Ubuntu 16.04 虚拟机上使用 apt-get,因为缺少 python3

问题描述 投票:0回答:2

我似乎找不到解决方案。我需要

sudo apt-get install <package>
才能工作,因为
sudo apt-get -f -qq install -y
不起作用。

这是日志:

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:~$ 

从日志中,我发现 python3 尚未安装。然而,运行

sudo apt-get install python3
似乎表明它已安装。这是日志:

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:~$ 

但是,检查 /usr/bin/ 后,我没有安装 python3 二进制文件。

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:~$ 

我尝试按照指示执行操作并运行

sudo apt-get install --reinstall python3
,但得到:

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:~$ 

现在,我尝试遵循许多声称在没有

sudo apt-get
命令的情况下在 ubuntu 16.04 上安装 python3 的文章,但似乎无法正确执行。有人可以发布有关如何修理我的机器的说明吗?

python python-3.x ubuntu ubuntu-16.04 python-install
2个回答
1
投票

可能是因为它已安装但不在路径上,所以 Ubuntu 找不到它。如果它说它已安装,请尝试使用which命令来查找它。

which python3

然后检查它是否在您的路径上

echo $PATH

如果没有将其添加到路径中使用

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

/home/dave/work 将是你的 Python 路径

编辑:它可能就像按照建议运行

sudo apt-get -f install
一样简单。


0
投票

我在 EC2 虚拟机中升级 Ubuntu 16 时遇到了类似的问题,因为软件包

sosreport
- 你的问题可能是我的一个突变,所以我希望这会有所帮助。

症状:

$ 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)

故障排除

看来软件包在安装后失败是因为

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

我们可以看到

py3compile
命令存在语法问题:

$ 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

让我们看看哪个包包含它:

$ 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

解决方案

所以只需升级 python 包即可:

$ sudo apt install python3-minimal

然后编译没有错误:

$ py3compile -p sosreport 

这样您就可以继续升级系统了:

sudo apt upgrade
© www.soinside.com 2019 - 2024. All rights reserved.