c00*_*ter 12 python wxpython python-import
我在Mac OS 10.6.4下运行Python 2.7,我刚刚从wxPython2.8-osx-unicode-2.8.11.0-universal-py2.7.dmg二进制文件中安装了wxPython .我import wx在Python脚本中遇到了一个奇怪的错误.仅供参考,我可以从PyCrust中轻松导入wx模块.我真的没有看到我在这里做错了什么.有人可以帮忙吗?
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
Run Code Online (Sandbox Code Playgroud)
看来这里包含的wxPython 2.7 dmg的C扩展模块只有32位.
$ 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
...
Run Code Online (Sandbox Code Playgroud)
遗憾的是,platform.architecture()没有准确指示运行OS X多架构Python的哪个arch.例如,使用Python 2.7的3-arch python.org安装程序,platform.architecture()即使在32位模式下运行,也始终报告64位:
$ 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
Run Code Online (Sandbox Code Playgroud)
可靠的方法是检查sys.maxintPython 2或sys.maxsizePython 3.
您没有在问题中指出如何调用Python.它是通过脚本文件中的shebang行吗?如果是这样,您可能无法运行您认为自己的Python.此外,您没有指出已安装的Python 2.7.例如,python.org目前有两个Python 2.7安装程序:一个支持32位和64位执行,另一个支持32位.请尝试以下方法:
$ 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
Run Code Online (Sandbox Code Playgroud)
所以:如果你有一个多拱的Python版本,你需要强制它以32位模式运行才能使用预编译的wxPython.