views:

166

answers:

2

I am running Python 2.7 under Mac OS 10.6.4, and I just installed wxPython from the wxPython2.8-osx-unicode-2.8.11.0-universal-py2.7.dmg binary. I am getting a weird error on the import wx line in my Python scripts. FYI, I can import the wx module just fine from PyCrust. I don't really see what I have done wrong here. Could anyone please assist?

  File "prod_cons_wx.py", line 6, in <module>
    import wx 
  File "/usr/local/lib/wxPython-unicode-2.8.11.0/lib/python2.7/site-packages/wx-2.8-mac-unicode/wx/__init__.py", line 45, in <module>
    from wx._core import *
  File "/usr/local/lib/wxPython-unicode-2.8.11.0/lib/python2.7/site-packages/wx-2.8-mac-unicode/wx/_core.py", line 4, in <module>
    import _core_
ImportError: dlopen(/usr/local/lib/wxPython-unicode-2.8.11.0/lib/python2.7/site-packages/wx-2.8-mac-unicode/wx/_core_.so, 2): no suitable image found.  Did find:
    /usr/local/lib/wxPython-unicode-2.8.11.0/lib/python2.7/site-packages/wx-2.8-mac-unicode/wx/_core_.so: no matching architecture in universal wrapper
A: 

How have you installed python on Snow Leopard OSX (10.6) Series? Is your python compiled for 64 bit or 32 bit.

Try doing the following:

import platform
print platform.architecture()

Check out if the binary (wxpython dmg) was compiled for 32 or 64 bit. You might have to look for a package that is compatible with your architecture or you might have to compile from source on your machine.

I would suggest that you use macports.

  1. install macports from macport.org
  2. sudo /opt/local/bin/port install python27
  3. sudo /opt/local/bin/port install python_select
  4. sudo /opt/local/python_select python27
  5. sudo /opt/local/bin/port install py27-wxpython

and this should work for you!

pyfunc
I seem to have Python 32 bit installed. The wxPython mac binaries don't say anything about 32 vs 64 bit. But the weird thing is that wxPython runs fine on my other mac, with identical system and installation. Only this one particular computer is kicking up a fuzz.
c00kiemonster
Just check the architecture on the other mac.I would suggest using the macports. It works fine as both python and wxpython are compiled on your machine. Also with this, you can switch from python26, 27 and 3!
pyfunc
The strange thing is they are both the same, i.e., 32 bit. Perhaps I should give macports a go then.
c00kiemonster
Note, `platform.architecture()` often does not give accurate results on OS X. See my answer for the details.
Ned Deily
+1  A: 

It appears that C extension modules included with the wxPython 2.7 dmg here are 32-bit only.

$ cd /usr/local/lib/wxPython-unicode-2.8.11.0/lib/python2.7/site-packages/wx-2.8-mac-unicode/wx
$ file *.so
_animate.so:   Mach-O universal binary with 2 architectures
_animate.so (for architecture ppc): Mach-O bundle ppc
_animate.so (for architecture i386):    Mach-O bundle i386
_aui.so:       Mach-O universal binary with 2 architectures
_aui.so (for architecture ppc): Mach-O bundle ppc
_aui.so (for architecture i386):    Mach-O bundle i386
...

Unfortunately, platform.architecture() does not give an accurate indication of which arch an OS X multiple architecture Python is running in. For example, using the 3-arch python.org installer for Python 2.7, platform.architecture() always reports 64-bit even when running in 32-bit mode:

$ cd /Library/Frameworks/Python.framework/Versions/2.7
$ file python2.7
python2.7: Mach-O universal binary with 3 architectures
python2.7 (for architecture i386):  Mach-O executable i386
python2.7 (for architecture ppc7400):   Mach-O executable ppc
python2.7 (for architecture x86_64):    Mach-O 64-bit executable x86_64
$ arch -x86_64 ./python2.7 -c 'import platform, sys; print "{0}, {1:x}".format(platform.architecture()[0], sys.maxint)'
64bit, 7fffffffffffffff
$ arch -i386 ./python2.7 -c 'import platform, sys; print "{0}, {1:x}".format(platform.architecture()[0], sys.maxint)'
64bit, 7fffffff
$ arch -ppc ./python2.7 -c 'import platform, sys; print "{0}, {1:x}".format(platform.architecture()[0], sys.maxint)'
64bit, 7fffffff

The reliable way is to examine sys.maxint for Python 2 or sys.maxsize for Python 3.

You don't indicate in your question how you invoke Python. Is it via a shebang line in a script file? If so, you may not be running the Python you think you are. Also, you don't indicate which Python 2.7 you have installed. For instance, there are currently two installers for Python 2.7 from python.org: one supports both 32- and 64-bit execution, the other is 32-bit only. Try the following:

$ file $(python2.7 -c 'import sys;print(sys.executable)')
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: Mach-O universal binary with 3 architectures
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python (for architecture i386):   Mach-O executable i386
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python (for architecture ppc7400):    Mach-O executable ppc
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python (for architecture x86_64): Mach-O 64-bit executable x86_64

So: if you have a multi-arch version of Python, you'll need to force it to run in 32-bit mode to make use of the pre-compiled wxPython.

Ned Deily
I am running the python script from a regular terminal window, i.e., '$ python script.py'. The import is done in the regular way, right at the beginning of the script. Printing sys.maxint gives 9223372036854775807 so I guess I am running in 64 bit mode. This is what I get when I try the last bit:../Python: Mach-O universal binary with 3 architectures../Python (for architecture i386): Mach-O executable i386../Python (for architecture ppc7400)Mach-O executable ppc../Python (for architecture x86_64):Mach-O 64-bit executable x86_64
c00kiemonster
I just checked the other mac where wxPython worked fine, sys.maxint shows that it is running 32 bit. How can I force python to run 32 bit mode?
c00kiemonster
You should be able to use `arch -i386 python2.7 script.py` to run in 32-bit mode. If that proves to be troublesome and you don't otherwise need 64-bit features, you could download and install the other python.org 2.7 installer (the one marked 10.3 and above). That will overwrite your existing python2.7 installation with a 32-bit only one. You may need to re-install the wxPython afterwards.
Ned Deily
It worked fine running it with the arch thing. However the 'export VERSIONER_PYTHON_PREFER_32_BIT=yes' as per http://stackoverflow.com/questions/2088569/how-do-i-force-python-to-be-32-bit-on-snow-leopard-and-other-32-bit-64-bit-questi didin't work. Is there any other way to force python to switch to 32 bit?
c00kiemonster
I just re-installed python with the 32 bit only binary. It works fine now. Got a bit to fiddly for me there. Thanks a lot for the help.
c00kiemonster
Glad it works for you now. By the way, `export VERSIONER_PYTHON_PREFER_32_BIT=yes` is a feature of the Apple-supplied Python 2.6 in OS X 10.6. It has no effect on the python.org Pythons.
Ned Deily