Python 3无法正常工作

我最近从源代码安装了python 3.5,但我认为在安装或链接过程中我做错了。 在此之后,我在安装任何Ubuntu软件包时遇到问题。 例如,即使在重新安装软件属性 – common之后,add-apt-repository也不起作用。

$ add-apt-repository bash: /usr/bin/add-apt-repository: /usr/bin/python3: bad interpreter: No such file or directory 

另外,apt-get install会给我这个错误

 $ sudo apt-get install libraw-dev Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: libraw-dev 0 upgraded, 1 newly installed, 0 to remove and 229 not upgraded. 3 not fully installed or removed. Need to get 382 kB/391 kB of archives. After this operation, 1,588 kB of additional disk space will be used. Get:1 http://us.archive.ubuntu.com/ubuntu/ trusty/main libraw-dev amd64 0.15.4-1 [382 kB] Fetched 382 kB in 0s (651 kB/s) Selecting previously unselected package libraw-dev:amd64. (Reading database ... 295477 files and directories currently installed.) Preparing to unpack .../libraw-dev_0.15.4-1_amd64.deb ... Unpacking libraw-dev:amd64 (0.15.4-1) ... Setting up python3.4 (3.4.3-1ubuntu1~14.04.5) ... Could not find platform independent libraries  Consider setting $PYTHONHOME to [:] Fatal Python error: Py_Initialize: Unable to get the locale encoding ImportError: No module named 'encodings' Aborted dpkg: error processing package python3.4 (--configure): subprocess installed post-installation script returned error exit status 134 Setting up python3-commandnotfound (0.3ubuntu12) ... /var/lib/dpkg/info/python3-commandnotfound.postinst: 6: /var/lib/dpkg/info/python3-commandnotfound.postinst: py3compile: not found dpkg: error processing package python3-commandnotfound (--configure): subprocess installed post-installation script returned error exit status 127 dpkg: error processing package software-properties-common (--configure): package is in a very bad inconsistent state; you should reinstall it before attempting configuration Setting up libraw-dev:amd64 (0.15.4-1) ... Errors were encountered while processing: python3.4 python3-commandnotfound software-properties-common E: Sub-process /usr/bin/dpkg returned an error code (1) 

此外,尝试添加ppa会出现以下错误

 $ sudo add-apt-repository ppa:dhor/myway sudo: unable to execute /usr/bin/add-apt-repository: No such file or directory 

发生了什么,有什么想法吗?

  1. 首先,您需要恢复Python 3的核心:

     sudo apt install -f --reinstall python3 python3.4 python3-minimal python3.4-minimal libpython3.4-minimal 

    如果由于半安装的软件包不起作用,取决于Python 3下载并手动安装它们:

     cd /tmp apt-get download python3 python3.4 python3-minimal python3.4-minimal libpython3.4-minimal sudo dpkg -i *python3*.deb sudo apt install -f 
  2. 安装debsums软件包并执行软件包完整性检查以validation其余软件包的完整性,并在必要时重新安装。

Ubuntu附带了自己的Python安装,它依赖于系统实用程序。 这些实用程序依赖于指向这些系统安装的pythonpython2python3 。 不幸的是,当从源代码构建/安装Python时,标准make install会安装新的python / python2 (对于Python 2.x)或python3 (对于Python 3.x)二进制文件,这最终会影响系统Python安装和破坏事物。

一旦你的系统得到修复(David的答案应该会让你继续),那么你应该使用make altinstall (重新)安装源代码编译的Python,它将放弃python3并仅将解释器安装为特定于版本的python3.5 。 这是安装自己的Python而不会干扰系统Pythons的安全方法,因为它不会影响系统实用程序所依赖的任何命令。