2010-11-06 58 views
2

以下爲this示例,我創建了一個hello.pyd庫文件,其內容在本問題的末尾。
在IronPython的解釋器(ipy.exe)中導入* .pyd庫

當我進入Python解釋器,我得到如下:

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

但隨着IronPython的解釋器嘗試這會產生一個錯誤:

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

我怎樣才能讓IPY解釋接受這個C++編譯庫?


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

編譯如下

python setup.py build -cmingw32 

回答

3

您可以嘗試使用Ironclad,但目前還沒有看到太多的工作最近。

0

答案很可能是您的.pyd庫不在IronPython的正確路徑中。由於您使用的是Python而不是IronPython的安裝工具,因此它可能是在PYTHONPATH中構建和設置的,而不是它需要用於IronPython的地方。

的解決方案是一個。)更改爲IronPython的或b路徑。)重建的IronPython的路徑

+3

沒有,問題是,.pyd是一個非託管C擴展,而IronPython的是有管理的.NET應用程序;你需要一個圖層來翻譯兩者,就像P/Invoke一樣。 – 2010-11-06 15:22:56

+0

@Ignacio:謝謝,這可能更有可能。儘管值得注意的是,無論使用什麼工具或使用何種工具,它都必須處於正常的工作狀態。 – 2010-11-06 15:31:56