In the course of updating a book for Python 3.0, I’ve had a lot of need to test and compare code in Python 3 and Python 2.6. The first is obvious, and the second is needed because Python 2.6’s -3 commandline switch is a key step in the process of converting code from the 2.x world to Python 3.
Python 3 vs Python 2.6
In this post I’ll talk about installing Python 3.0 from source. Python 2.6 works much the same way except that you need to exercise some caution to make sure that 2.6 doesn’t replace 2.5 as the default Python. This is a bad thing because all the Python packages and libraries you install by way of Ubuntu packages are tied to the default Python 2.5 installed automatically. If 2.6 becomes your default Python interpreter, suddenly you lose all of those packages. Python 3.0 on the other hand will not set itself up as the default Python interpreter unless you explicitly tell it to.
The problem with Ubuntu
The problem with Ubuntu is that I’ve been running it for a few years and I’m spoiled. Using synaptic or apt-get to install software has become second nature to me, and it’s been a while since I installed any significant package from source. So when I needed the latest Python installed, my automatic reaction was to look for packages.
The bad news wasn’t unexpected – there are no Python 2.6 packages in the standard repo’s, which is really no surprise since 2.6 wasn’t released until after Intrepid. However, there were packages for Python 3. Beta and RC versions, to be sure, but there were packages. The problem was that while the base Python 3 package installs just fine, there seems to be no compatible Tkinter package. No Tkinter, no IDLE. Since the book I’m revising makes some mention of IDLE, this was a deal-breaker for me.
Back to the source
So out of necessity, I figured out how to make source installs of Python 3 and Python 2.6 (mostly) happy on Ubuntu 8.10. If you’re familiar with compiling from source on Linux/UNIX, just grab the packages listed below and get to it. OTOH, if you’re new to compiling, I’ve given more complete directions below…
In general, you need the following packages installed before you try to build Python from source:
- build-essential – the compiler and tools to build pretty much anything.
- libsqlite3-dev libreadline5-dev libncurses5-dev zlib1g-dev libbz2-dev libssl-dev libgdbm-dev and tk-dev – these packages (and their dependencies) are needed to build various Python libraries. Note that you need the “-dev” versions. Having libsqlite3 installed, for example, won’t be enough to build the Python libraries.
If you’re already at a terminal prompt the fast way to handle the above is:
doc@x60:~$ sudo apt-get install build-essential libsqlite3-dev libreadline5-dev libncurses5-dev zlib1g-dev libbz2-dev libssl-dev libgdbm-dev tk-dev
If you installed the Python 3 Ubuntu packages, you should uninstall them before going on.
Installing Python 3.0
- First you need to get the Python 3.0 source from http://www.python.org/download/ – choose either of the compressed source tarballs and save to your home directory (or other convenient spot).(For the following steps you need to be in a terminal window)
- The next step is to unpack the tarball – switch to the folder where you saved the tarball and…
tar xvf Python-3.0.tgz
- The tarball should be unpacked into a folder called Python-3.0, so switch there…
doc@x60:~$ cd Python-3.0
- There are 3 steps to compiling from source – configuring the package, compiling, and then installing. You configure the source package with the configure command;
doc@x60:~/Python-3.0$ ./configure(the “./” is important here)
This command checks your system and sets up the correct options for the build. It should end with a statement “Creating Makefile”. If there are errors instead, they must be fixed before you go any further.
- The compile step is pretty easy – you give the “make” command:
The make command will take some time, since it has to compile everything. Again, if the make ends with an error, you must fix the error to go on.
- The install step is the big one, since it actually puts Python 3 on your system. By default Python 3.0 will not replace your existing Python, so it’s safe to go for it:
doc@x60:~/Python-3.0$ sudo make install
You need to use sudo because the install will try to place the files in spots where a normal user doesn’t have access – by default under /usr/local/.
After this, you should have python3.0 and idle3.0 available from the commandline. Creating menu entries,shortcuts, etc, I’ll leave as an exercise for the reader.