明确不同版本的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.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.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.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.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.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 $ 

我该怎么做才能得到python2 python3? 如何使用apt-get重新安装python?

在大多数Ubuntu安装中,Pythons 2和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软件包解压缩到系统中的位置(它们只是带有标题数据的拉链)。

如果您刚刚编写了/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中存在一个需要修复的错误才能创建一个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路径。