OSX端口Python库路径?无法找到ctypes

时间:2010-10-12 17:29:16

标签: python macos path

我正在尝试使用具有ctypes依赖关系的应用程序,但我收到此错误:

$ python peach.py -t ~/Desktop/fuzz/wav/template.xml 

] Peach 2.3.6 Runtime
] Copyright (c) Michael Eddington

Traceback (most recent call last):
  File "peach.py", line 335, in <module>
    from Peach.Engine import *
  File "/opt/Peach-2.3.6/Peach/__init__.py", line 40, in <module>
    import Publishers, Transformers
  File "/opt/Peach-2.3.6/Peach/Publishers/__init__.py", line 37, in <module>
    import file, sql, stdout, tcp, udp, com, process, http, icmp, raw, remote, dll, smtp
  File "/opt/Peach-2.3.6/Peach/Publishers/file.py", line 37, in <module>
    from Peach.Engine.engine import Engine
  File "/opt/Peach-2.3.6/Peach/Engine/engine.py", line 835, in <module>
    from Peach.Engine.state import StateEngine
  File "/opt/Peach-2.3.6/Peach/Engine/state.py", line 38, in <module>
    import sys, re, types, time, struct, ctypes
  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

我已经从ports安装了py-ctypes,但它似乎只是一个Python 2.4版本:

$ port contents py-ctypes
Port py-ctypes contains:
  /opt/local/lib/python2.4/site-packages/_ctypes.so
  /opt/local/lib/python2.4/site-packages/_ctypes_test.so
  /opt/local/lib/python2.4/site-packages/ctypes/__init__.py
  /opt/local/lib/python2.4/site-packages/ctypes/__init__.pyc
  /opt/local/lib/python2.4/site-packages/ctypes/_endian.py
  /opt/local/lib/python2.4/site-packages/ctypes/_endian.pyc
  /opt/local/lib/python2.4/site-packages/ctypes/macholib/__init__.py
  /opt/local/lib/python2.4/site-packages/ctypes/macholib/__init__.pyc
  /opt/local/lib/python2.4/site-packages/ctypes/macholib/dyld.py
  /opt/local/lib/python2.4/site-packages/ctypes/macholib/dyld.pyc
  /opt/local/lib/python2.4/site-packages/ctypes/macholib/dylib.py
  /opt/local/lib/python2.4/site-packages/ctypes/macholib/dylib.pyc

然后我尝试通过python2.4运行应用程序,但似乎应用程序使用仅在2.5中可用的语法:

$ python2.4 peach.py -t ~/Desktop/fuzz/wav/template.xml 
  File "peach.py", line 498
    finally:
          ^
SyntaxError: invalid syntax

我的python安装也来自OSX端口,我注意到在Peach应用程序中,它将python定义为:

#!/usr/bin/python

如果我的默认python可执行文件指向我的端口安装(并且我正在运行'python peach.py​​'),那会不会有任何问题?

$ which python
/opt/local/bin/python

有没有解决这个问题?

  • ctypes for python2.5?
  • 能够将2.4库添加到2.5路径吗?

2 个答案:

答案 0 :(得分:2)

一个简单的解决方案是使用Mac OS附带的本机Python构建。这绝对适用于最新版本的Mac OS X 10.6.4,它有Python 2.6。

以下示例显示正在成功导入“_ctypes”:

mariah:~ joet3ch$ /usr/bin/python
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from _ctypes import Union, Structure, Array
>>>

如果您在此之后遇到问题,请尝试查看sys.path属性以查看路径中的模块和版本。

以下是在全新Mac OS 10.6.4版本中查看sys.path内容的示例:

mariah:~ joet3ch$ /usr/bin/python
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload', '/Library/Python/2.6/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC', '/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/wx-2.8-mac-unicode']
>>> 

答案 1 :(得分:1)

ctypes是自2.5版以来的标准Python库,因此不需要py-ctypes。在我的2.6.5安装中,您获得ImportError的行仍然存在。

我不拥有OSX,所以我的问题是:/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5是否属于Python 2.5的标准安装,或者这可能属于某个框架可能已损坏的安装?

在正在运行的python shell中,您可以检查sys.path的值。也许在标准库之前有一些破碎的库。

如果它是像普通程序一样直接调用的可执行脚本的第一行,则操作系统会解释shebang行#!/usr/bin/python。在所有其他情况下,这只是一个评论。特别是,如果您在python myscript.py中调用脚本或者由其他Python代码导入脚本,则会忽略该行。