gpt4 book ai didi

c++ - 在 IronPython 的解释器 (ipy.exe) 中导入 *.pyd 库

转载 作者:搜寻专家 更新时间:2023-10-31 01:58:25 25 4
gpt4 key购买 nike

正在关注 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

最佳答案

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

关于c++ - 在 IronPython 的解释器 (ipy.exe) 中导入 *.pyd 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4113643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com