64-bit Python on Macs
Posted in Apple, Python on June 7th, 2008 by stan / No Comments »There was a question recently on the ROOT mailing list where someone was having a problem using the python executable that comes with Mac OS X 10.5 and 64-bit libraries. I went digging around, and noticed a strange discrepancy. The compiled python libraries that ship with Leopard are four architecture universal binaries:
stan@Rover:/usr/lib/python2.5/config$ file libpython2.5.a
libpython2.5.a: Mach-O universal binary with 4 architectures
libpython2.5.a (for architecture ppc7400):
Mach-O dynamically linked shared library ppc
libpython2.5.a (for architecture ppc64):
Mach-O 64-bit dynamically linked shared library ppc64
libpython2.5.a (for architecture i386):
Mach-O dynamically linked shared library i386
libpython2.5.a (for architecture x86_64):
Mach-O 64-bit dynamically linked shared library x86_64
(Reformatted to avoid spilling into my sidebar…)
However, the python executable is not compiled for 64-bit architectures:
stan@Rover:/usr/bin$ file python2.5
python2.5: Mach-O universal binary with 2 architectures
python2.5 (for architecture ppc7400):
Mach-O executable ppc
python2.5 (for architecture i386):
Mach-O executable i386
I hadn’t noticed this since my MacBook is the early Core Duo model, rather than Core 2 Duo, so the hardware does not support x86_64. Apple may have good reasons to force all python scripts to run as 32-bit applications even on 64-bit systems, but I don’t know what they are.
If you find yourself wanting 64-bit python, it’s very easy to make your own, since all the Python libraries on Leopard are already 32/64-bit universal. Just go grab the very short python64.c from the ROOT svn repository, and compile it like this:
gcc -arch ppc64 -arch x86_64 -arch i386 -arch ppc \
-o python64 -I/usr/include/Python2.5 -lpython2.5 python64.c
(Note that this has nothing to do with the ROOT libraries. If you have no idea what ROOT is, the above will still work.)
Now you can check the python64 executable:
Rover:tmp stan$ file python64
python64: Mach-O universal binary with 4 architectures
python64 (for architecture ppc64):
Mach-O 64-bit executable ppc64
python64 (for architecture x86_64):
Mach-O 64-bit executable x86_64
python64 (for architecture i386):
Mach-O executable i386
python64 (for architecture ppc7400):
Mach-O executable ppc
All four architectures are now present. I haven’t got a 64-bit Mac to try this out on, so I don’t know if it actually runs correctly there. Being universal, this binary works just fine on my 32-bit Mac, of course.
