5

我正在尝试导入 ctypes,并且我正在使用使用 macports 安装的 Python 2.5.5(在 Mac OS X 10.6 上)。

我收到一条错误消息“ImportError:没有名为 _ctypes 的模块”(请参阅​​下面的详细信息)。据我了解,ctypes 应该是为 python 2.5 预装的。有什么建议么?

谢谢,索拉布

错误详情:

$ python
Python 2.5.5 (r255:77872, Nov 30 2010, 00:05:47) 
[GCC 4.2.1 (Apple Inc. build 5659)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/ctypes/\_\_init\_\_.py", line 10, in <module>
    from _ctypes import Union, Structure, Array
ImportError: No module named _ctypes

更新:我检查了/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload目录,没有 Ned 的回复。没有 _ctypes.so 文件,但是有两个相关文件:

-rwxr-xr-x  2 root  wheel  136176 Nov 30 00:07 _ctypes_failed.so
-rwxr-xr-x  2 root  wheel   26160 Nov 30 00:05 _ctypes_test.so

这是否意味着我需要重新安装 ctypes?

更新 2:重新安装 python 2.5 解决了这个问题。不太确定是什么导致了原始问题。

4

3 回答 3

1

可能是文件权限问题。我没有方便的 MacPorts Python 2.5 安装,但是基于 MacPorts Python 2.6,应该有一个类似这样的文件:

$ cd /opt/local/Library/Frameworks/Python.framework/Versions/2.5
$ cd ./lib/python2.5/lib-dynload/
$ ls -l _ctypes.so 
-rwxr-xr-x  2 root  wheel  151536 Oct  8 00:26 _ctypes.so*

ls -l如果没有,请使用上述目录的结果更新您的问题lib-dynload

更新:根据您的附加信息,存在_ctypes_failed.so表示_ctypes在构建和安装 Python 2.5 期间构建失败。尝试使用详细模式重新安装它以查看失败的原因:

$ port sudo selfupdate
$ port -v upgrade --force python25
于 2010-12-27T07:14:01.283 回答
1

最新的 Mac OS X 附带 Python 2.6。尝试使用它而不是 macports。导入 ctypes 已经在我的系统上工作。

Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>>
于 2010-12-27T02:00:38.013 回答
0

安装 libffi,然后重建 Python。

于 2010-12-27T00:46:05.877 回答