Python 2.7 on Ubuntu
I am new to Python and am working on a Linux machine (Ubuntu 10.10). It is running python 2.6, but I'd like to run 2.7 as it has features I want to use. I have been urged to not install 2.7 and set that as my default python.
My question is, how can I install 2.7 and run it s开发者_开发知识库ide by side with 2.6?
I did it with pythonbrew on my Ubuntu 10.10 machine.
$ 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.
I recently backported Python 2.7 to Debian squeeze. Since Ubuntu 10.10 is newer than Debian squeeze, if you can do it on squeeze, you can certainly do it on Ubuntu. I don't have access to a Ubuntu 10.10 system. If I set one up, I'll test on it, and update this answer. So, here instead is a brief sketch of what I did on Debian.
First, a general and obvious comment, but something that is easily overlooked. One should not take the listed build dependencies of a Debian package too seriously. They may be far more specific than needed. For example, software like Python, which is designed to be portable and run over a wide array of systems, is unlikely to build depend on very specific versions of software. The runtime dependencies can be adjusted as well, but this should be done with more caution. However, runtime dependencies are mostly generated dynamically based on software that is already on this system, so usually that is not a big issue.
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
Selecting the testing version we get
apt-get source python2.7=2.7.2-8
Looking at debian/control
, we see the following build dependency lines.
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], 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 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
Most of this is easily satisfied on squeeze. With the handy utility apt-show-versions
we get on my machine
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
We see that everything except libtinfo-dev
is available in squeeze. I do have the squeeze backport versions of debhelper
and python-sphinx
, but both of these are also available for debian squeeze in versions satisfying the build requirements.
Observe also that I have libncurses5-dev installed
apt-show-versions libncurses5-dev
libncurses5-dev/squeeze uptodate 5.7+20100313-5
Both of these packages correspond to the source package curses 5.7+20100313-5
. Observe that libtinfo-dev
in fact replaces 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)
One would not expect python 2.7 to develop on such a specific version of curses, and in fact it doesn't. However, if you try to build the packages without satisfying the dependency you get
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.
So, it is necessary to edit debian/control
. Note that you also need to similarly edit the file debian/control.in
, otherwise the control
file will be incorrectly regenerated from control.in
. The simplest thing to do is just remove libncurses5-dev
from the Build-Conflicts
line and libtinfo-dev
from the Build-Depends
line, and then run debuild -uc -us
again. If you are going to have this package installed alongside the standard default Python 2.6 packages on Debian squeeze, you
also need to remove the two lines
Conflicts: python-profiler (<= 2.7.1-2)
Replaces: python-profiler (<= 2.7.1-2)
Those lines are there because 2.7 includes the python-profiler
functionality.
If 2.7 is the default python, then python-profiler
is no longer necessary.
However, if one is installing 2.7 as a non-default Python, that reasoning does not
apply, and python-profiler
is still needed by 2.6.
This should build successfully, and result in the following list of binary packages.
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
Finally, one can install the binary packages with
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
Sometimes dpkg
can be a little difficult about satisfying dependencies when they are all installed at once, so you might have to run apt-get -f install
afterwards if you get dependency errors, or alternatively install the packages in smaller groups.
Well if the only thing you need is argparse (saw that in one of your comments!) you could just do :
pip install argparse
This is not exactly an answer to the exact question :-) , but indeed if you are only missing a few feature, many 2.7 features actually come from independent projects and/or some compatibility packages can be found, eg:
- argparse
- Which implementation of OrderedDict should be used in python2.6? : OrderedDict on pypi
- you should be able to replace memoryview with buffer
The list of 2.7 novelties is admittedly longer, but most of the other new features are probably not a big miss, and in exchange you do not mess around with multiple python installations on your box. Otherwise go with pythonbrew :-)
ubuntu 12.04
Install dependencies:
$ sudo apt-get install python-software-properties
Add the repo:
$ sudo add-apt-repository ppa:fkrull/deadsnakes
Update the repo index:
$ sudo apt-get update
Install Python 3.3:
$ sudo apt-get install python3.3
ubuntu 12.04 > more
Installing the dependencies:
$ sudo apt-get install build-essential libsqlite3-dev sqlite3 bzip2 libbz2-dev
Download and compile 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
Some nice touches to install a py command by creating a symlink:
$ mkdir ~/bin
$ ln -s /opt/python3.3/bin/python ~/bin/py
Just download Python 2.7 from http://www.python.org/download/releases/2.7.1/ and build it.
You can use virtualenv to create distinct Python environments. Just being newsy, but what does Python 2.7 have that you need?
Another option is to install ActivePython if you do not want to compile things yourself. It includes a binary package manager as well.
精彩评论