72

我是 Python 新手,正在使用 Linux 机器(Ubuntu 10.10)。它正在运行 python 2.6,但我想运行 2.7,因为它具有我想要使用的功能。我被敦促不要安装 2.7 并将其设置为我的默认 python。

我的问题是,如何安装 2.7 并与 2.6 并排运行?

4

7 回答 7

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

于 2011-03-08T14:16:22.070 回答
10

我最近将 Python 2.7 反向移植到 Debian 挤压。由于 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,我们看到以下构建依赖行。

Build-Depends: 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], 语言环境 [!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 Build-Depends-Indep: python-sphinx Build-Conflicts: tcl8.3 -dev、tk8.3-dev、tcl8.4-dev、tk8.4-dev、python2.7-xml、python-xml、autoconf2.13、libncurses5-dev

其中大部分很容易在挤压时得到满足。使用方便的实用程序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挤压之外的所有东西。我确实有 和 的挤压后向移植版本debhelperpython-sphinx但是这两个版本也可用于满足构建要求的 debian 挤压版本。

还要注意我安装了 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. 最简单的做法就是从线路中移除libncurses5-devBuild-Conflictslibtinfo-dev线路中移除Build-Depends,然后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-profiler2.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如果出现依赖项错误,您可能必须在之后运行,或者将包安装在较小的组中。

于 2012-01-19T18:54:47.160 回答
5

好吧,如果您唯一需要的是 argparse (在您的一条评论中看到了这一点!),您可以这样做:

pip install argparse

这并不完全是对确切问题的回答 :-),但实际上,如果您只缺少一些功能,许多 2.7 功能实际上来自独立项目和/或可以找到一些兼容性包,例如:

不可否认, 2.7的新功能列表更长,但大多数其他新功能可能并不是什么大问题,作为交换,你不会在你的盒子上安装多个 python 。否则使用 pythonbrew :-)

于 2011-05-23T15:52:53.603 回答
3

Ubuntu 12.04

安装依赖项:

$ sudo apt-get install python-software-properties

添加回购:

$ sudo add-apt-repository ppa:fkrull/deadsnakes

更新回购索引:

$ sudo apt-get update

安装 Python 3.3:

$ sudo apt-get install python3.3

ubuntu 12.04 > 更多

安装依赖项:

$ sudo apt-get install build-essential libsqlite3-dev sqlite3 bzip2 libbz2-dev

下载并编译python:

$ wget http://python.org/ftp/python/3.3.0/Python-3.3.0.tar.bz2

$ tar jxf ./Python-3.3.0.tar.bz2

$ cd ./Python-3.3.0

$ ./configure --prefix=/opt/python3.3

$ make && sudo make install

通过创建符号链接来安装 py 命令的一些不错的方法:

$ mkdir ~/bin
$ ln -s /opt/python3.3/bin/python ~/bin/py
于 2013-07-15T11:11:29.313 回答
1

只需从http://www.python.org/download/releases/2.7.1/下载 Python 2.7并构建它。

于 2011-03-08T14:10:03.663 回答
1

如果您不想自己编译,另一种选择是安装ActivePython 。它还包括一个二进制包管理器

于 2011-03-11T05:07:18.673 回答
1

您可以使用virtualenv创建不同的 Python 环境。只是新闻,但是 Python 2.7 有什么你需要的?

于 2011-03-08T14:28:37.607 回答