August 3, 2009 at 4:09 PM by Dr. Drang
Python 3.1 for the Mac comes as a
.dmg file which, when mounted, has a rather spartan set of contents.
Running the installer and accepting its defaults1, creates and fills a new
3.1 subdirectory in
/Library/Frameworks/Python.framework/Versions and puts a
python3 command in
/usr/local/bin. Everything associated with Python 3.1 is distinct from the standard Python 2.5 that ships with Leopard. The
python command still starts up version 2.5
$ python Python 2.5.1 (r251:54863, Feb 6 2009, 19:02:12) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
and the python3 command starts up Python 3.1
$ python3 Python 3.1 (r31:73578, Jun 27 2009, 21:49:46) [GCC 4.0.1 (Apple Inc. build 5493)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
One thing that I expected to happen was the creation of a new
3.1/site-packages directory tree under
2.5/site-packages directory tree is where third-party modules for Python 2.5 go, but the installer didn’t create a corresponding tree for 3.1.
I suspect that running
python3 setup.py on a module packaged with Python’s distutils will create the
3.1/site-packages tree, but the first module I wanted to install was Stuart Colville’s titlecase, which just comes as a single Python source file to be moved somewhere where Python can find it.
[Aside: I had some hope that the
title() method for strings in Python 3.1 would have been improved with regard to apostrophes. It doesn’t seem too much to ask that
"don't pass me by".title()
Don't Pass Me By
instead of the nonsensical
Don'T Pass Me By
Sadly, 3.1 is just as stupid as 2.5. The titlecase module is much smarter, and even knows to keep small words like articles and (most) prepositions uncapitalized unless they’re the first or last word of the title. It’s based on a Perl script by John Gruber described in this blog post.]
It turns out that simply creating a
3.1/site-packages tree in
/Library/Python is sufficient to add it to Python 3.1’s module search path. Here are the commands to run:
cd /Library/Python/ sudo mkdir -p 3.1/site-packages sudo chmod -R g+w 3.1
-p switch tells
mkdir to create all the necessary intermediate directories. They’ll have an owner of
root and a group of
chmod command allows all users in the
admin group—which includes my usual login user account—to add files to the new directories. Without this, the
.pyc bytecode files can’t be made, and the modules will have to be compiled from source every time they’re imported.
One more thing: the titlecase module was written for Python 2.x; it needs one change to make it 3.1-compatible. Change Line 249 in
before you install it.
I do wish Python, like Ruby, allowed base classes to be extended—
titlecase would be more natural as a string method than a function.