views:

53

answers:

2

I am trying to use an application that has a dependency of ctypes, but am getting this error:

$ 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

I have installed py-ctypes from ports, but it seems to only be a Python 2.4 version:

$ 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

I then tried to run the application via python2.4, but it seems the application uses syntax that is only available in 2.5:

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

My python install is also from OSX ports, and I noticed in the Peach application, it defines python as:

#!/usr/bin/python

Will that mess with anything if my default python executable points to my port installation (and I am running 'python peach.py')?

$ which python
/opt/local/bin/python

Is there any work around for this?

  • ctypes for python2.5?
  • Ability to add 2.4 libraries to 2.5 path?
+1  A: 

ctypes is a standard Python library since version 2.5, so py-ctypes is not needed. The line at which you get an ImportError still exists in my 2.6.5 installation.

I do not own OSX, so my question is: does /opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5 belong to the standard installation of Python 2.5, or may this belong to a possibly broken installation of some framework?

In a running python shell, you could inspect the value of sys.path. Maybe there is some broken library that precedes the standard library.

The shebang line #!/usr/bin/python is interpreted by the OS if it is the first line of an executable script that is invoked directly like an ordinary program. In all other cases, this is just a comment. In particular, the line is ignored if you invoke the script as in python myscript.py or if it is imported by other Python code.

Bernd Petersohn
+1  A: 

A simple solution would be to use the native Python build that is included with Mac OS. This definitely works with the latest release of Mac OS X 10.6.4, which has Python 2.6.

Here is an example showing that '_ctypes' is being imported successfully:

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
>>>

If you have issues after this, try looking at the sys.path attribute to see which modules and versions are in your path.

Here is an example of viewing the contents of sys.path on a fresh Mac OS 10.6.4 build:

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']
>>> 
joet3ch