【问题标题】:Importing a *.pyd library in IronPython's interpreter (ipy.exe)在 IronPython 的解释器 (ipy.exe) 中导入 *.pyd 库
【发布时间】:2010-11-06 15:10:45
【问题描述】:

按照this 的例子,我创建了一个小hello.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

【问题讨论】:

    标签: c++ python ironpython python-extensions


    【解决方案1】:

    您可以尝试使用Ironclad,但最近没有看到太多工作。

    【讨论】:

      【解决方案2】:

      答案很可能是您的 .pyd 库不在 IronPython 获取它的正确路径中。由于您使用的是 Python 而不是 IronPython 的设置工具,因此它可能是在 PYTHONPATH 中构建和设置的,而不是 IronPython 所需的位置。

      解决方案是 a.) 更改 IronPython 的路径或 b.) 在 IronPython 的路径中重建

      【讨论】:

      • 不,问题是 .pyd 是非托管 C 扩展,而 IronPython 是托管 .NET 应用程序;你需要一个层在两者之间进行转换,就像 P/Invoke 一样。
      • @Ignacio:谢谢,很可能是这样。尽管值得注意的是,无论他使用什么工具或使用什么工具,它都必须处于正确的工作路径。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多