Ubuntu上的Python 2.7

71 投票
7 回答
58195 浏览
提问于 2025-04-16 13:15

我刚接触Python,现在在一台Linux机器上(Ubuntu 10.10)。这台机器上运行的是Python 2.6,但我想用2.7,因为它有我想用的功能。有人建议我不要把2.7安装成默认的Python版本。

我想问的是,怎么才能安装2.7,并且让它和2.6一起运行呢?

7 个回答

5

如果你只需要 argparse(我在你的一条评论中看到过这个!),你可以直接这样做:

pip install argparse

这并不是对你问题的确切回答:-),但确实,如果你只是缺少一些功能,很多 Python 2.7 的功能其实来自独立项目,或者可以找到一些兼容包,比如:

虽然 2.7 的新特性 列表确实更长,但大多数其他新功能可能并不是特别重要,反而可以避免在你的电脑上搞多个 Python 安装。否则就用 pythonbrew 吧:-)

10

我最近把Python 2.7移植到了Debian squeeze上。因为Ubuntu 10.10比Debian squeeze更新,所以如果你能在squeeze上做到这点,肯定也能在Ubuntu上做到。我没有Ubuntu 10.10的系统,如果我搭建一个,我会在上面测试,并更新这个回答。所以这里我简单介绍一下我在Debian上做的事情。

首先,有一点很重要,但容易被忽视。我们不应该太认真对待Debian包中列出的构建依赖。它们可能比实际需要的要具体得多。比如,像Python这样的软件,设计上是为了在各种系统上都能运行,所以它不太可能依赖于某个特定版本的软件。运行时依赖也可以调整,但这需要更加小心。不过,运行时依赖大多是根据系统上已经存在的软件动态生成的,所以通常这不是个大问题。

apt-cache policy python2.7
python2.7:
  Installed: 2.7.2-8
  Candidate: 2.7.2-8
  Version table:
     2.7.2-12 0
         50 http://debian.csail.mit.edu/debian/ unstable/main i386 Packages
     2.7.2-8 0
         50 http://debian.csail.mit.edu/debian/ testing/main i386 Packages
 *** 2.7.2-8 0
        100 /var/lib/dpkg/status

选择我们要的测试版本

apt-get source python2.7=2.7.2-8

查看debian/control文件,我们可以看到以下构建依赖的内容。

构建依赖:debhelper(>= 5),quilt,autoconf,libreadline-dev,libtinfo-dev,libncursesw5-dev(>= 5.3),tk8.5-dev,zlib1g-dev,blt-dev(>= 2.4z),libssl-dev,libexpat1-dev,sharutils,libbz2-dev,libbluetooth-dev [linux-any],locales [!armel !avr32 !hppa !ia64 !mipsel],libsqlite3-dev,libffi-dev(>= 3.0.5),mime-support,libgpm2 [linux-any],netbase,lsb-release,bzip2,libdb4.8-dev,gdb,python,help2man 独立构建依赖:python-sphinx 构建冲突:tcl8.3-dev,tk8.3-dev,tcl8.4-dev,tk8.4-dev,python2.7-xml,python-xml,autoconf2.13,libncurses5-dev

在squeeze上大部分依赖都能满足。使用一个方便的工具apt-show-versions,我在我的机器上得到了

apt-show-versions debhelper quilt autoconf libreadline-dev libtinfo-dev libncursesw5-dev tk8.5-dev zlib1g-dev blt-dev \
libssl-dev libexpat1-dev sharutils libbz2-dev libbluetooth-dev locales libsqlite3-dev \
libffi-dev mime-support libgpm2 netbase lsb-release bzip2 libdb4.8-dev gdb python help2man python-sphinx

autoconf/squeeze uptodate 2.67-2
blt-dev/squeeze uptodate 2.4z-4.2
bzip2/squeeze uptodate 1.0.5-6
debhelper/squeeze-backports uptodate 8.9.13~bpo60+1
gdb/squeeze uptodate 7.0.1-2+b1
help2man/squeeze uptodate 1.38.2
libbluetooth-dev/squeeze uptodate 4.66-3
libbz2-dev/squeeze uptodate 1.0.5-6
libdb4.8-dev/squeeze uptodate 4.8.30-2
libexpat1-dev/squeeze uptodate 2.0.1-7
libffi-dev/squeeze uptodate 3.0.9-3
libgpm2/squeeze uptodate 1.20.4-3.3
libncursesw5-dev/squeeze uptodate 5.7+20100313-5
libreadline-dev/squeeze uptodate 6.1-3
libsqlite3-dev/squeeze uptodate 3.7.3-1
libssl-dev/squeeze uptodate 0.9.8o-4squeeze5
libtinfo-dev not installed
locales/squeeze uptodate 2.11.2-10
lsb-release/squeeze uptodate 3.2-23.2squeeze1
mime-support/squeeze uptodate 3.48-1
netbase/squeeze uptodate 4.45
python/squeeze uptodate 2.6.6-3+squeeze6
python-sphinx/squeeze-backports uptodate 1.0.8+dfsg-2~bpo60+1
quilt/squeeze uptodate 0.48-7
sharutils/squeeze uptodate 1:4.9-1
tk8.5-dev/squeeze uptodate 8.5.8-1
zlib1g-dev/squeeze uptodate 1:1.2.3.4.dfsg-3

我们看到除了libtinfo-dev之外,其他的都可以在squeeze上找到。我确实有squeeze的回移植版本的debhelperpython-sphinx,而且这两个在满足构建要求的版本下也可以在Debian squeeze上找到。

还要注意我已经安装了libncurses5-dev

apt-show-versions libncurses5-dev

libncurses5-dev/squeeze uptodate 5.7+20100313-5

这两个包对应的源包是curses 5.7+20100313-5。注意libtinfo-dev实际上是替代了libncurses5-dev

apt-cache show libtinfo-dev

Package: libtinfo-dev
Source: ncurses
Version: 5.9-4
Installed-Size: 279
Maintainer: Craig Small <csmall@debian.org>
Architecture: i386
Replaces: libncurses5-dev (<< 5.9-3)
Depends: libtinfo5 (= 5.9-4)

我们不应该指望Python 2.7依赖于如此特定版本的curses,实际上它并不依赖。不过,如果你尝试在不满足依赖的情况下构建包,你会得到

debuild -uc -us

dpkg-checkbuilddeps: Unmet build dependencies: libtinfo-dev
dpkg-checkbuilddeps: Build conflicts: libncurses5-dev
debuild: fatal error at line 1289:
You do not appear to have all build dependencies properly met.
You can use mk-build-deps to generate a dummy package which
Depends on all the required packages, or you can install them
manually using dpkg or apt using the error messages just above
this message.

因此,有必要编辑debian/control文件。注意你还需要类似地编辑debian/control.in文件,否则control文件会错误地从control.in重新生成。最简单的方法就是从Build-Conflicts行中删除libncurses5-dev,从Build-Depends行中删除libtinfo-dev,然后再次运行debuild -uc -us。如果你打算在Debian squeeze上与标准的默认Python 2.6包一起安装这个包,你还需要删除这两行

Conflicts: python-profiler (<= 2.7.1-2)
Replaces: python-profiler (<= 2.7.1-2)

这些行是因为2.7包含了python-profiler功能。如果2.7是默认的Python,那么python-profiler就不再必要了。不过,如果你是把2.7作为非默认的Python安装,这个理由就不适用了,python-profiler仍然是2.6所需要的。

这样应该可以成功构建,并生成以下的二进制包列表。

ls -lah *.deb

-rw-r--r-- 1 faheem staff 289K Jan 12 02:33 idle-python2.7_2.7.2-8_all.deb
-rw-r--r-- 1 faheem staff 1.1M Jan 12 02:34 libpython2.7_2.7.2-8_i386.deb
-rw-r--r-- 1 faheem staff 2.5M Jan 12 02:34 python2.7_2.7.2-8_i386.deb
-rw-r--r-- 1 faheem staff  12M Jan 12 02:34 python2.7-dbg_2.7.2-8_i386.deb
-rw-r--r-- 1 faheem staff 4.9M Jan 12 02:34 python2.7-dev_2.7.2-8_i386.deb
-rw-r--r-- 1 faheem staff 6.0M Jan 12 02:33 python2.7-doc_2.7.2-8_all.deb
-rw-r--r-- 1 faheem staff 692K Jan 12 02:33 python2.7-examples_2.7.2-8_all.deb
-rw-r--r-- 1 faheem staff 1.7M Jan 12 02:34 python2.7-minimal_2.7.2-8_i386.deb

最后,可以用以下命令安装这些二进制包。

dpkg -i python2.7-minimal_2.7.2-8_i386.deb python2.7_2.7.2-8_i386.deb python2.7-dev_2.7.2-8_i386.deb libpython2.7_2.7.2-8_i386.deb 

有时候dpkg在一次性满足所有依赖时可能会有点困难,所以如果你遇到依赖错误,可能需要在之后运行apt-get -f install,或者选择分批安装这些包。

108

我在我的Ubuntu 10.10电脑上用pythonbrew完成了这个。

$ python -V
Python 2.6.6
$ curl -kL https://raw.github.com/utahta/pythonbrew/master/pythonbrew-install | bash
$ . $HOME/.pythonbrew/etc/bashrc
$ pythonbrew install 2.7.1
$ pythonbrew switch 2.7.1
Switched to Python-2.7.1
$ python -V
Python 2.7.1

我还用它安装了Python 3.2,具体可以看这个链接

撰写回答