views:

1327

answers:

6
+5  A: 

The problem is that WxPython is only available on the Mac in 32-bit mode; however, by default, Python will start up in 64-bit mode. To fix this problem, create the following shell script named python2.6_32:

#! /bin/bash
export VERSIONER_PYTHON_PREFER_32_BIT=yes
/usr/bin/python2.6 "@"

Make the script executable (chmod a+x python2.6_32) and place the script in your path. Now, simply invoke python2.6_32 to get an interactive Python console in which you can use WxPython. If you want to write a Python script that uses this, you can use the shebang: #! /usr/bin/env python2.6_32.

Now to explain... the basic problem is that 32-bit and 64-bit code uses a different application binary interface (ABI), and so 32-bit code and 64-bit code cannot coexist in the same library/executable/process. In order to support 64-bit mode, it needs to have been compiled in 64-bit mode; likewise, to support 32-bit mode, it needs to have been compiled in 32-bit mode. Under OS X, it is possible, using universal binaries to support both... however, it needs to be compiled in both modes (and then merged). WxWidgets probably uses Carbon, which is only available in 32-bit mode (Cocoa is available in both 32-bit and 64-bit mode... Apple didn't bother making Carbon available in both modes, since it is being deprecated), which would explain why WxPython, in turn, could only be provided in 32-bit mode. This, in turn, means that using it in Python requires you to launch Python in 32-bit mode (Python is a universal binary with both 32-bit and 64-bit versions of itself available in the same binary file, so it can be launched in either mode).

Alternative Option
I don't recommend doing this, because I think you should leave the defaults as they are, but since you might not have enough shell scripting knowledge (you need to use "./python2.6_32" or place it in a folder that is listed in your "$PATH" environment variable and invoke it as "python2.6_32") to follow the former option, you might want to simply execute the following command which will make 32-bit mode the default:

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

If you decide you want to switch back into 64-bit mode, you can then use the following command:

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

Note that both commands are to be executed on the Terminal (not within Python).

Source
I should point out that both recomendations are based on man python on Mac OS X. So, if you have any other questions, you should definitely read the man page as the error message has urged you to do.

Michael Aaron Safyan
I get a "command not found" when trying to execute python2.6_32 from terminal.
Alex
@Alex, you need to create the script, make it executable, and put it in your path. Have you done those steps? It doesn't exist out of the box.
Michael Aaron Safyan
@Alex, can you tell me what you are typing on the commandline, whether you have created the script as instructed, where the script is if you have created it, and what the value of "$PATH" is?
Michael Aaron Safyan
Thank you! I have been unable to get it working, even with a self-compiled-as-64-bit wxPython and Python 2.7. Your method worked.
ראובן
+1  A: 

Another solution is to download and install Python 2.6 for OS X from python.org and install wxPython for OS X from here with it. The python.org 2.6 is newer (2.6.5 as of now) than the Apple-supplied Python (2.6.1) in Snow Leopard and it is 32-bit only.

Ned Deily
@Ned, that's a bad idea... everything that is necessary is already installed, and that would overshadow the 64-bit version.
Michael Aaron Safyan
I am still getting "no appropriate 64-bit archetecture" when trying this method
Alex
No, it would not "overshadow" the 64-bit version. It is perfectly fine to have multiple Python versions installed on an OS X system. And, as I mentioned, there is the advantage of having the latest fixes in 2.6.5. The key to using multiple versions is managing your execution PATH properly. To use the python.org version of 2.6, ensure `/Library/Frameworks/Python.framework/Versions/2.6/bin` comes before `/usr/bin` in $PATH. The python.org package installs a script command (`/Applications/Python 2.6/Update Shell Profile.command`) that will modify `.bash_profile` and `.profile` for you.
Ned Deily
@Alex: you need to ensure you are using the python.org python. As noted above, you can update your shell profile using the command. Or there is likely an alias to that python that was installed as `/usr/local/bin/python2.6`. Do not attempt to change the Apple-supplied Python at `/usr/bin/python2.6`.
Ned Deily
@Ned, I said "overshadow" and not "overwrite", because it would, indeed, overshadow the pre-installed version (while leaving the original intact). Also, if you are going to deal with multiple versions of Python, then using MacPorts to do it and using "python_select" would make the most sense... but that is complete overkill since the version that ships with Mac OS X is perfectly capable of running in 32-bit mode.
Michael Aaron Safyan
@Michael: I find the "overshadow" distinction a bit too fuzzy but fine. And I should have emphasized the "another solution" in my answer; that is, your recommendation works, too. I do think there is something to be said to moving to 2.6.5 as there have been a lot of fixes to python in general and to OS X support since the Apple 2.6.1 and the wxPython OS X installer specifically supports this use case. Also, I did not bring up MacPorts here because that introduces another set of issues: old version of wxPython, default 64-bit operation, etc.
Ned Deily
A: 

Hm. The script provided didn't work for me-- I changed it as follows:

#! /bin/bash
echo "-----------------Python 2.6 - 32 Bit setup --------------------"
echo "Running" $1
export VERSIONER_PYTHON_PREFER_32_BIT yes
/usr/bin/python2.6 $1

Still didn't work. I get the same message. Re-read the man page to make sure I wasn't misunderstanding, and I'm no further forward:

ImportError: /usr/local/lib/wxPython-unicode-2.8.10.1/lib/python2.6/site-packages/wx-2.8-mac-unicode/wx/_core_.so: no appropriate 64-bit architecture (see "man python" for running in 32-bit mode)

Not really sure why this doesn't work, unless there is some sort of rebuild that needs to be done against the wx core that gives it 32/64-bit compatibility. Any suggestions, anyone? I'd like to use the out-of-the-box Python install from Apple (be easier for my work), and I'd like to avoid any more ridiculous hacks

Doc
A: 

You might also want to try arch command when invoking python:arch -i386 /usr/bin/python2.6 if you can't get Python to run with the correct environmental settings. The '-i386' switch makes a universal binary run in Intel 32-bit mode. '-x86_64' makes it run in Intel 64-bit mode. -ppc and -ppc64 is for PPC architectures.

If you still get errors then it might point to a compile issue. On my machine I have the stock apple Python and a version from Macports. The arch command works using the apple binaries and I can import wx successfully from the command line but I still get errors from the Macports binary: Bad CPU type in executable I'm guessing I'll have to go back and recompile my Macports python binary and make sure it produces a universal binary or something like that (sigh).

spade78
A: 

This worked for me (from http://www.python-forum.de/viewtopic.php?f=19&t=24322&view=previous)

In .profile, add the following line alias py32='arch -i386 /Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7'

then invoke your script with py32

Prashant John
A: 

While I see this is already answered, the answer is slightly wrong. The 2.9 series DOES have a Mac 64-bit build, albeit only for Python 2.7. See http://wxpython.org/download.php and look for the Cocoa build. From what I gather on the wxPython mailing list and IRC channel, you'll want to download a Python 64-bit build from python.org rather than using the Mac-included snake.

Mike Driscoll