05.06.08
Python 3000
Last year, I learned to program in Python. In just a few months, I’d written pcsets: pitch class sets for python. However, pcsets has been stuck at version 2.0.2 for a good while now. There are two good reasons:
- That module was actually pretty well written. I’ve been using it as a pitch class set calculator all this time, and never encountered a bug or unexpected problem. I don’t really believe in the philosophy of “improving” something just to say it’s been improved.
- Meanwhile, the Python language itself is actually being improved — for real — with Python 3000.
I’ve been following the progress of Python 3000 for some time, but really only wanted to start working with it when the beta release seemed to be around the corner.
That time is now.
Today, I compiled and installed Python 3.0 alpha 4. However, this is still an alpha stage product, so I was very careful to make sure I didn’t accidentally torpedo my existing installation of Python 2.5.2.
Here’s how I did it
My system
Kubuntu Linux, newly upgraded to 8.04. The python version I use is the Ubuntu version, which lives in /usr/bin. From time to time, I may compile a newer version in /usr/local/bin, if the new version hasn’t made it into the repositories yet.
However, that places both areas off limits for an alpha. Instead of either location, I chose /opt. Here’s how:
./configure --prefix=/opt make sudo make altinstall
When this was done, I had installed the files and libraries under /opt. Here’s the directory listing for /opt/bin:
mccosar (1) -> ~/Python/3000 $ ls /opt/bin 2to3 idle pydoc python3.0 python3.0-config smtpd.py
There’s the converter program, 2to3, which should transform version 2 code into version 3. I haven’t tried it yet. The ‘altinstall’ option added the new python binary as ‘python3.0′. However, I can’t help but note that ‘idle’ and ‘pydoc’ both would have clobbered existing installs! (Now you see why I’m so careful all the time . . . .)
That was the only bugbear.
Here’s my official first Python 3000 script, ever. Note the altered form of the print() function — a function now, not a statement.
#!/opt/bin/python3.0
import sys
for fact in [sys.version, sys.path]:
print(fact)
Notice the first line, the special shebang I’ve used to point to the new interpreter. This script is just a sanity check — I’m checking to see if I’m talking to the correct interpreter, and the interpreter is looking in the right places for the right libraries.
mccosar (1) -> ~/Python/3000 $ ./template.py 3.0a4 (r30a4:62119, May 6 2008, 21:22:03) [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] ['/home/mccosar/Python/3000', '/opt/lib/python30.zip', '/opt/lib/python3.0', '/opt/lib/python3.0/plat-linux2', '/opt/lib/python3.0/lib-tk', '/opt/lib/python3.0/lib-dynload', '/opt/lib/python3.0/site-packages']
Looks like mission accomplished to me!
And so begins the next version of pcsets — version 3.0. I may not have improved the algorithms, but I’ve learned a few tricks that will make the module easier to use.