4

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

I get an error message when I try to import it usin 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 named PYTHONPATH which stores the path to the abcPython.dll, but still the error remains.

How can I fix this?

user240840
  • 51
  • 1
  • 1
  • 3

4 Answers4

13

Follow Building C and C++ Extensions on Windows carefully - in sub-section 7, it says:

The output file should be called spam.pyd (in Release mode) or spam_d.pyd (in Debug mode). The extension .pyd was chosen to avoid confusion with a system library spam.dll to which your module could be a Python interface
...
Changed in version 2.5: Previously, file names like spam.dll (in release mode) or spam_d.dll (in debug mode) were also recognized.

Try the renaming your DLL to use a .pyd extension instead of .dll.

(thanks, Peter Hansen)

The reference points to a C example, which explicitly includes an INIT function, PyMODINIT_FUNC initexample(void). The resulting DLL should be renamed 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);
}
Community
  • 1
  • 1
gimel
  • 83,368
  • 10
  • 76
  • 104
  • Thanks for replying I renamed the .dll to .pyd and then tried the following >>> import abcPython Traceback (most recent call last): File "", line 1, in ImportError: dynamic module does not define init function (PyInit_abcPython) >>> – user240840 Jan 07 '10 at 12:40
  • 1
    Follow instructions in the linked reference and DEFINE an init function! – gimel Jan 07 '10 at 13:24
  • Anybody asked the OP what version of Python he's using? – John Machin Jan 08 '10 at 13:48
2

Aarrgghh! Yet another 2.X/3.X gotcha. RTFErrorMessage:

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

Note the prefix: it's not init, it's PyInit_

See the 3.1 docs ... "The initialization function must be named PyInit_name(), where name is the name of the module"

John Machin
  • 81,303
  • 11
  • 141
  • 189
1

Simple renaming of .dll to .pyd did not help. I was using SWIG to create the extension module. I created a .pyd instead of creating a .dll module and that solved the issue.

cdeszaq
  • 30,869
  • 25
  • 117
  • 173
user240840
  • 51
  • 1
  • 1
  • 3
0

As an example: Imagine you have compiled OpenCV and have several *.dll and the cv2.pyd file.

You need to copy those files to 'DLLs' folder within the python directory.

Then import the module to check wether it is ok.

Nuno Aniceto
  • 1,892
  • 1
  • 15
  • 16