2

Following this example, I've created a little hello.pyd library file, the contents of which are at the end of this question.

When I enter python interpreter I get the following:

D:\test\build\lib.win32-2.6>C:\Python26\python.exe
Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello
>>> hello.say_hello("Greg")
Hello Greg!
>>>

But trying this with IronPython's interpreter yields an error:

D:\test\build\lib.win32-2.6>"C:\Program Files (x86)\IronPython 2.7\ipy.exe"
IronPython 2.7 Alpha 1 (2.7.0.1) on .NET 4.0.30319.1
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named hello
>>>

How can I make ipy interpreter accept this C++ compiled library?


hellomodule.cpp

#include "C:\Python26\include\Python.h"

static PyObject* say_hello(PyObject* self, PyObject* args)
{
    const char* name;

    if (!PyArg_ParseTuple(args, "s", &name))
        return NULL;

    printf("Hello %s!\n", name);

    Py_RETURN_NONE;
}

static PyMethodDef HelloMethods[] =
{
     {"say_hello", say_hello, METH_VARARGS, "Greet somebody."},
     {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC

inithello(void)
{
     (void) Py_InitModule("hello", HelloMethods);
}

setup.py

from distutils.core import setup, Extension

module1 = Extension('hello', sources = ['hellomodule.cpp'])

setup (name = 'PackageName',
        version = '1.0',
        description = 'This is a demo package',
        ext_modules = [module1])

Compiled as follows

python setup.py build -cmingw32
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359

2 Answers2

3

You can try using Ironclad, but it hasn't seen much work recently.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

The answer is most likely that your .pyd library isn't in the correct path for IronPython to pick it up. Since you used Python and not IronPython's setup tools, it probably got built and setup in the PYTHONPATH rather than where it needs to be for IronPython.

The solution is to a.) change your path for IronPython or b.) rebuild in IronPython's path

Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
  • 3
    No, the problem is that a .pyd is an unmanaged C extension, whereas IronPython is a managed .NET application; you need a layer to translate between the two, much like P/Invoke. – Ignacio Vazquez-Abrams Nov 06 '10 at 15:22
  • @Ignacio: thanks, that's probably more likely to be it. Though it's worth noting that it will have to be in the proper path to ever work regardless or what tools he's using. – Rafe Kettler Nov 06 '10 at 15:31