37

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.

Bialecki
  • 30,061
  • 36
  • 87
  • 109

7 Answers7

30
  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;-).

Ned Deily
  • 83,389
  • 16
  • 128
  • 151
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • 24
    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 Jan 19 '10 at 02:48
  • 7
    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 Aug 31 '10 at 10:36
  • 1
    Hi folks, I have built my python distribution from source using the Homebrew package manager project (their tag lines makes be laugh, "MacPorts driving you to drink? Try Homebrew!"). The VERSIONER_PYTHON_PREFER_32_BIT seems to me for the Apply supplied python, is there anyway of playing the same trick with a custom python distribution? I don't really want to alias 'arch -i386 python' to python. – Daniel Farrell Feb 06 '11 at 10:32
  • @boyfarrell I have the exact same question. Did you ever find a trick? Even aliasing didn't seem to work for me, I couldn't get my Django test server to run with 32-bit python – Neil Dec 11 '15 at 09:31
24

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
  • 4,050
  • 3
  • 26
  • 32
19

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
  • 303
  • 2
  • 5
6

The answer has been accepted. But I think the 3rd question is still unanswered. I found it to be an interesting question and so did some research for the same. Here is the answer I found in another SO forum -

SnowLeopard runs 32-bit system software and applications on 32-bit Intel machines, and 64-bit system software and 32-bit and 64-bit applications on 64-bit Intel machines. The desktop version of Snow Leopard boots a 32-bit kernel for reasons of kext and driver compatibility, but all user space runs 64-bit. Mac OS X Server boots into a 64-bit kernel. Core 2 Duo is a 64-bit machine. Try $ sysctl hw.cpu64bit_capable or sysctl hw.optional.x86_64 to verify that you have a 64-bit cpu. arch will always show i386 on Intel hardware in both Leopard and Snow Leopard. – cdespinosa Sep 22 '09 at 4:23

Original link - Mac OSX 10.6 compiler: a puzzling experience with 32 vs 64 bit

Community
  • 1
  • 1
Sumod
  • 3,806
  • 9
  • 50
  • 69
0

the third question has to do with the 32 / 64 bit kernel

just because you have a 64 bit capable OS and 64 bit capable CPU processor does not mean you are booting up a 64 bit kernel in Mac OSX

If you got a Mac with 10.6 snow leopard on it from the factory, then it shipped in 32 bit mode by default

you would have to edit the system setting to boot 64 bit mode

(not sure on changes to 10.7 or 10.8 as the latest I use is snow leo but I believe they may boot 64 bit kernel by factory-default)

10.6 - from terminal run the command:

 :*sudo nvram boot-args="arch=x86_64"

OR

10.6 - from terminal run the command:

sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.Boot 'Kernel Flags' 'arch=x86_64'
hackg
  • 119
  • 1
  • 3
  • 7
0

I tried many different ways of executing the Skype4Py test script using,

arch -i386

What worked in the end was looking at the segfault path for the claimed 64-bit version of python and putting that in my header file: #!/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python

After I put this line at the top and added execute permissions to my file, I could use, arch -i386 skype.py

And it would run without issue.

elec3647
  • 513
  • 5
  • 8
0

After a huge amount of trial and error myself trying out some of the suggestions above, I stumbled upon the following symlink:

/usr/local/bin/python2-32

pointing to:

/Library/Frameworks/Python.framework/Versions/2.7/bin/python2-32

And I observe that when I run this Python, it starts in 32-bit mode (in contrast to /Library/Frameworks/Python.framework/Versions/2.7/bin/python2). This can be observed in Activity Monitor.

Note: as others have pointed out elsewhere, platform.architecture() is not always a good indicator. It is showing '64-bit' for this 32-bit process.

Pyderman
  • 14,809
  • 13
  • 61
  • 106