views:

1296

answers:

3

When I run the following from a bash shell on my Mac:

$ file /usr/bin/python

I get the following three lines:

/usr/bin/python (for architecture x86_64):  Mach-O 64-bit executable x86_64
/usr/bin/python (for architecture i386):    Mach-O executable i386
/usr/bin/python (for architecture ppc7400): Mach-O executable ppc

this would seem to indicate that Python has been compiled for all three architectures or something like that? I believe, based on some errors I had while trying to setup MySQL, that the version I'm using is the 64-bit version. So two questions:

  1. How would I have known that?

  2. How could I change Python to be 32-bit instead? Something less drastic than re-compile with different compile settings?

  3. Why does arch from a bash shell return i386 which would seem to indicate I'm not in "64-bit mode" when I know based on my processor I'm running a 64-bit Mac?

Sorry these are probably all newbie questions, the whole 32/64-bit thing is frustrating the crap out of me and I'm sure there are some commands/tools that would make this easier.

+6  A: 

http://www.jaharmi.com/2009/08/29/python_32_bit_execution_on_snow_leopard

$ defaults write com.apple.versioner.python Prefer-32-Bit -bool yes

danielrsmith
That, or use the `VERSIONER_PYTHON_PREFER_32_BIT` environment variable as described in `man 1 python`.
Ned Deily
+7  A: 
  1. You can find out a lot about the Python version you're running via the platform module (the sys module also has a few simple helpers)

  2. On Mac OS X, you can run a "fat binary" with your chosen architecture with, for example,

    arch -i386 /usr/bin/python

I do not recommend altering /usr/lib/python itself (with the lipo command) -- you could easily make your system unusable by tampering with system files. Maybe installing a separate Python from python.org (for application purposes) while leaving the system Python alone is an acceptable strategy to you -- it's definitely safer than altering system files!-)

As for your third question, hmmm, this one's a stumper to me -- and definitely a question for superuser.com (as well as completely unrelated to Python, it also seems completely unrelated to programming;-).

Alex Martelli
While `arch -i386 filename` will generally do the right thing, on 10.6 `arch -i386 /usr/bin/python` will still run Python in 64-bit mode (if possible). Either use `export VERSIONER_PYTHON_PREFER_32_BIT=yes` (see Apple's `man 1 python`) or `arch -i386 /usr/bin/python2.6`.
Ned Deily
Note the VERSIONER_PYTHON_PREFER_32_BIT environment variable is a feature of the Apple-supplied Python 2.6 in OS X 10.6 (`/usr/bin/python`). It has no affect on other Pythons such as those installed by python.org installers.
Ned Deily
+3  A: 

Fix for use with virtualenv on Snow Leopard

danielrsmith's answer works for me when I am not using virtualenv, but virtualenv makes a copy of the python executable which causes it not to work:

$ which python
/Users/cogg/.virtualenvs/tweakeats/bin/python

$ python
[...]
>>> import sys
>>> sys.maxint
9223372036854775807

because this is a copy of python, I used lipo on it to remove the 64-bit architecture. This allows me to use 32-bit python 2.6 with virtualenv:

$ lipo -info /Users/cogg/.virtualenvs/tweakeats/bin/python
Architectures in the fat file: /Users/cogg/.virtualenvs/tweakeats/bin/python are: x86_64 i386 ppc7400
$ mv /Users/cogg/.virtualenvs/tweakeats/bin/python /Users/cogg/.virtualenvs/tweakeats/bin/python.old
$ lipo -remove x86_64 /Users/cogg/.virtualenvs/tweakeats/bin/python.old -output /Users/cogg/.virtualenvs/tweakeats/bin/python
$ python
[...]
>>> import sys
>>> sys.maxint
2147483647
cogg