清楚python不同版本的安装情况

清楚python不同版本的安装情况

我在 ubuntu 上安装 python 时遇到了令人困惑的情况。

$ python --version
Python 3.4.0
$ python2 --version
Python 3.4.0
$ python3 --version
Python 3.4.0
$ ls -la /usr/bin/python2
lrwxrwxrwx 1 root root 9 Dez 21  2013 /usr/bin/python2 -> python2.7
$ ls -la /usr/bin/python3
lrwxrwxrwx 1 root root 9 Mär 23  2014 /usr/bin/python3 -> python3.4

我只想在命令“python”下执行 python3.4。所以我将 /usr/bin/python3 复制到 /usr/bin/python,因为python --version返回 2.7,现在返回 3.4。我不知道这是否是一个错误。

我尝试使用 pip 安装一个包,但失败了。然后我尝试重新安装 python ( sudo apt-get install --reinstall python),终端提示我运行“apt-get -f install”,但不带包名。

$ sudo apt-get -f install
Reading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
8 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Setting up python2.7 (2.7.6-8) ...
  File "/usr/lib/python2.7/py_compile.py", line 114
    except Exception,err:
                    ^
SyntaxError: invalid syntax
dpkg: error processing package python2.7 (--configure):
 subprocess installed post-installation script returned error exit status 1
dpkg: dependency problems prevent configuration of python:
 python depends on python2.7 (>= 2.7.5-1~); however:
  Package python2.7 is not configured yet.

dpkg: error processing package python (--configure):
 dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of python-dateutil:
 python-dateutil depends on python (>= 2.7); however:
  Package python is not configured yet.
 python-dateutil depends on python (<< 2.8); however:
  Package python is not configured yet.
 python-dateutil depends on python:any (>= 2.7.1-0ubuntu2); however:
  Package python is not configured yet.

dpkg: error processing package python-dateutil (--configure):
 dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of python-gst-1.0:
 python-gst-1.0 depends on python (>= 2.7); however:
  Package python is not configured yet.
 python-gst-1.0 depends on python (<< 2.8); however:
  Package python is not configured yet.
 python-gst-1.0 depends oNo apport report written because the error message indicates its a followup error from a previous failure.
                                                                                                                                   No apport report written because the error message indicates its a followup error from a previous failure.
                                              No apport report written because MaxReports is reached already
                                                                                                            No apport report written because MaxReports is reached already
                                                                                                                                                                          No apport report written because MaxReports is reached already
                                         No apport report written because MaxReports is reached already
                                                                                                       No apport report written because MaxReports is reached already
                                                                                                                                                                     n python:any (>= 2.7.1-0ubuntu2); however:
  Package python is not configured yet.

dpkg: error processing package python-gst-1.0 (--configure):
 dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of python-pyparsing:
 python-pyparsing depends on python (>= 2.7); however:
  Package python is not configured yet.
 python-pyparsing depends on python (<< 2.8); however:
  Package python is not configured yet.
 python-pyparsing depends on python:any (>= 2.7.1-0ubuntu2); however:
  Package python is not configured yet.

dpkg: error processing package python-pyparsing (--configure):
 dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of python-tz:
 python-tz depends on python (>= 2.7); however:
  Package python is not configured yet.
 python-tz depends on python (<< 2.8); however:
  Package python is not configured yet.
 python-tz depends on python:any (>= 2.7.1-0ubuntu2); however:
  Package python is not configured yet.

dpkg: error processing package python-tz (--configure):
 dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of python-support:
 python-support depends on python (>= 2.5); however:
  Package python is not configured yet.

dpkg: error processing package python-support (--configure):
 dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of python-matplotlib:
 python-matplotlib depends on python-dateutil; however:
  Package python-dateutil is not configured yet.
 python-matplotlib depends on python-pyparsing; however:
  Package python-pyparsing is not configured yet.
 python-matplotlib depends on python-tz; however:
  Package python-tz is not configured yet.
 python-matplotlib depends on python (<< 2.8); however:
  Package python is not configured yet.
 python-matplotlib depends on python (>= 2.7); however:
  Package python is not configured yet.
 python-matplotlib depends on python-support (>= 0.90.0); however:
  Package python-support is not configured yet.

dpkg: error processing package python-matplotlib (--configure):
 dependency problems - leaving unconfigured
Errors were encountered while processing:
 python2.7
 python
 python-dateutil
 python-gst-1.0
 python-pyparsing
 python-tz
 python-support
 python-matplotlib
E: Sub-process /usr/bin/dpkg returned an error code (1)
$ 

我已经尝试将链接改回来:

$ sudo cp /usr/bin/python2.7 /usr/bin/python
$ python --version
Python 3.4.0
$ sudo cp /usr/bin/python2 /usr/bin/python
$ python --version
Python 3.4.0
$

我应该怎么做才能获得 python2python3?如何使用 apt-get 重新安装 python?

答案1

在大多数 Ubuntu 安装中,Python 2 和 Python 3 可以和谐共存。您所描述的(python3映射到 Python 2 二进制文件)根本不正常。

python 需求默认映射到 Python 2。有多种脚本不符合 Python 3(不向后兼容),因此如果您破坏映射python,则会破坏系统。

您已经从需要 Python 2 的包的 postinst 脚本中看到了这一点。

以下是我的python映射方式(14.04 安装):

$ readlink -f $(which python)
/usr/bin/python2.7

因此让我们重新映射/usr/bin/python回去:

sudo rm /usr/bin/python
sudo ln -s /usr/bin/python{2.7,}

然后运行您的sudo apt-get -f install程序,现在应该能够无错误地运行。


如果您把事情搞得如此糟糕,您可能会发现自己需要手动将 Python 包解压到系统中(它们只是带有标头数据的 zip 文件)。

如果你刚刚下载了/usr/bin/python2.7(应该是 Python 2 二进制文件),你可以通过下载 python2.7-minimal 包并将二进制文件解压到正确的位置来替换它:

apt-get download python2.7-minimal
ar x python2.7-minimal_*.deb data.tar.xz
sudo tar xJf data.tar.xz -C / './usr/bin/python2.7'
rm data.tar.xz

这是基于当前的 14.04 软件包。如果您使用的是其他版本,则可能需要调整路径。或者从运行相同版本 Ubuntu 的实时系统复制。


更广泛地说,如果你想要一个 Python 开发环境,我强烈建议你看看模块venv。这与 Py2 的 VirtualEnv 类似,只不过它是内置的。您可以以非 root、非系统的方式安装任何您喜欢的东西,包括映射python到 Python 二进制文件挑选(包括Pypy)。

在 14.04 版中,Python 3 中有一个 bug,需要修复在你创建 venv 之前,但目前可以很容易地解决这个问题。我们只需要安装ensurepip

wget -qO- http://d.pr/f/YqS5+ \
| sudo tar xzf - -C $(python3 -c "import sys; print(sys.path[1])") --no-same-owner

然后创建并激活 venv:

python3 -m venv myvenv
source ./myvenv/bin/activate

现在您已进入自己的游乐场。您将需要在将来调用 activate(或明确调用 myvenv/bin/python 二进制文件)来加载正确的 Python 路径。

相关内容