Import Python dll extensions

I created extensions for my Python and created abcPython.dll. How to import this dll into my python scripts?

I get an error when trying to import it into the following command

>>>import abcPython
>>>Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
     ImportError: No module named abcPython
>>>

I manually created a system environment variable with a name PYTHONPATHthat stores the path to abcPython.dll, but still the error remains.

How can i fix this?

+3
source share
4 answers

Follow Building C and C ++ Extensions on Windows - section 7 says:

spam.pyd( Release) spam_d.pyd( ). .pyd , spam.dll, Python
...
2.5: , spam.dll( ) spam_d.dll( ).

DLL .pyd .dll.

(, )

C , INIT, PyMODINIT_FUNC initexample(void). DLL example.pyd:

#include "Python.h"

static PyObject *
ex_foo(PyObject *self, PyObject *args)
{
    printf("Hello, world\n");
    Py_INCREF(Py_None);
    return Py_None;
}

static PyMethodDef example_methods[] = {
    {"foo", ex_foo, METH_VARARGS, "foo() doc string"},
    {NULL, NULL}
};

PyMODINIT_FUNC
initexample(void)
{
    Py_InitModule("example", example_methods);
}
+12

Aarrgghh! 2.X/3.X . RTFErrorMessage:

ImportError: dynamic module does not define init function (PyInit_abcPython)

: init, PyInit _

3.1 docs... " PyInit_name(), name "

+2

Just renaming the .dll to .pyd did not help. I used SWIG to create an extension module. I created .pyd instead of creating the .dll module and solved the problem.

+1
source

As an example: imagine you compiled OpenCV and have several * .dll and cv2.pyd files.

You need to copy these files to the dll folder in the python directory.

Then import the module to check if this is normal.

0
source

Source: https://habr.com/ru/post/1726885/


All Articles