当前位置: 首页>>技术教程>>正文


Ubuntu上的Python 2.7

, ,

问题描述

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

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

最佳办法

我在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

I also used it to install Python 3.2

次佳办法

最近,我将Python 2.7移植到Debian压缩版中。由于Ubuntu 10.10比Debian压缩版本新,因此如果您可以在压缩版本上进行压缩,那么您当然可以在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],语言环境[!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.in错误地重新生成control文件。最简单的操作是从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,则该推理将不适用,并且2.6仍需要python-profiler

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

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,或者将软件包安装在较小的组中。

第三种办法

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

pip install argparse

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

2.7 novelties的列表肯定更长,但是其他大多数新功能可能都不是一个大错失,作为交换,您不会在盒子上安装多个python。否则请使用pythonbrew 🙂

第四种办法

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

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/10450.html,未经允许,请勿转载。