How to get both 32-bit and 64-bit Python in Snow Leopard

We had some discussion on one of the Python issues about whether my Python in Snow Leopard should be 32-bit or 64-bit. I originally thought that it was tied to what the kernel was, but I turned out to be wrong.

From what I discovered, the important thing is what the Python was compiled as. You can tell what your Python has been compiled as by running:

>>> import sys
>>> from math import log
>>> log(sys.maxsize, 2)

If this is just under 31, then it is 32 bit. If it returns 63, then it is 64. An easier way to tell it to run:

>>> 2**40

If you get 1099511627776L, then you have 32-bit Python, if you get 1099511627776, you have 64-bit Python (notice that the number is long in 32-bit Python, because it is larger than maxint).

This test won’t work in Python 3 because all integers are “long” by default, but the first part will still work.

So why does this matter, you ask? Well, aside from the fact that much longer numbers are not long (anything less than 2**63 – 1 = 9223372036854775807), there is the issue of hashing.

In 64-bit Python:

>>> hash('a')

but in 32-bit Python

>>> hash('a')

SymPy uses hash values to order arguments, so often it happens that behavior in one architecture will not show up in the other. These problems are often hard to track and fix, but the worst is when things work fine on the machine you are working on. This actually happened to me with my GSoC project. I was renumbering the arbitrary constants in the printing order in an expression, but it turned out that the printing order of an expression can be dependent on .args order, so I had to modify the tests to canonize the numbering first.

So here comes the crux of the post. It turns out that on Mac OS X, if you install the binary from (Mac Installer Disk Image), this installs a 32-bit Python (for compatibility reasons) in /Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6
. However, if you install Python using 64-bit fink in Snow Leopard, it will compile it from source into 64-bit, and install it into /sw/bin/python2.6.

So now I have an easy way to test both architectures without having to ssh into some other machine, which was what I was doing before.


9 Responses to How to get both 32-bit and 64-bit Python in Snow Leopard

  1. shanipribadi says:

    I think what you meant was log(sys.maxsize, 2) so that it returns the base 2 logarithm of sys.maxsize. Or you could also use log(sys.maxsize)/log(2)

  2. Fred Palmer says:

    This is a little better way to determine your version:

    NullVoid ~: python
    Python 2.6.5 (r265:79359, Mar 24 2010, 01:32:55)
    [GCC 4.0.1 (Apple Inc. build 5493)] on darwin
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import struct
    >>> struct.calcsize(“P”) * 8
    >>> quit()
    NullVoid ~: /System/Library/Frameworks/Python.framework/Versions/2.6/bin/python
    Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
    [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import struct
    >>> struct.calcsize(“P”) * 8

  3. Aaron Meurer says:

    How is it better? It doesn’t seem simpler to me, especially not simper than the 2**40 trick. It does explain why strings hash differently, though. sys.maxsize seems to me to be the most future-proof of the lot. If I wanted to check this in a script, I would definitely use that one.

    Also, I noticed that Python 2.7 will be offered in both 32- and 64-bit Mac OS X installers. I don’t know if it will be possible to install both System installs at the same time, though.

  4. […] This link shows how to identify if a python that I’m running is 32bit or 64bit. And I checked my python is 32 bit. […]

  5. […] How to get both 32-bit and 64-bit Python in Snow Leopard November 20095 comments and 1 Like on, 3 […]

  6. […] you do get 64-bit Python setup using the methods outlined above above, I also found this blog post by Aaron Meurer helpful for verifying that Python is in fact installed as 64-bit. The post also talks about running […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: