【问题标题】:ctypes import not working on python 2.5ctypes导入不适用于python 2.5
【发布时间】:2010-12-26 22:48:36
【问题描述】:

我正在尝试导入 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 解决了这个问题。不太确定是什么导致了原始问题。

【问题讨论】:

  • 哇,我不知道人们还在使用 2.5。
  • @Rook 的人仍然使用 Python 2.4.x。
  • @Rook 在 Red hat、CentOS 和其他一些服务器发行版上,拥有 2.5 甚至 2.4 仍然很常见。 Google 不使用任何超过 2.5 的版本(事实上,要开发 App Engine,您必须以 2.5 为目标)。
  • 它可能与路径变量有关。 this post 的解决方案是删除 APPDATA 环境变量。也许这可以解决您的问题?
  • @marcog 我没有任何名为 APPDATA 的环境变量。 @Rook 由于某些依赖关系,我需要 python2.5。导入 ctypes 实际上在我的 python2.6 系统上运行良好。

标签: python macos ctypes python-2.5


【解决方案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*

如果不是,请使用上面lib-dynload 目录的ls -l 的结果更新您的问题。

更新:根据您的补充信息,_ctypes_failed.so 的存在表明在 Python 2.5 的构建和安装过程中 _ctypes 的构建失败。尝试使用详细模式重新安装它以查看失败的原因:

$ port sudo selfupdate
$ port -v upgrade --force python25

【讨论】:

    【解决方案2】:

    最新的 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
    >>>
    

    【讨论】:

      【解决方案3】:

      安装 libffi,然后重建 Python。

      【讨论】:

        猜你喜欢
        • 2018-01-23
        • 2023-04-07
        • 1970-01-01
        • 2017-08-20
        • 2022-08-05
        • 1970-01-01
        • 2015-08-13
        • 2012-04-06
        • 2023-02-21
        相关资源
        最近更新 更多