4

I have embedded a Python 2.7.2 interpreter into a C++ application using the Python C API.

On the target machines, I can't guarantee a Python install, so I am trying to get the embedded interpreter to look at the folder where my application resides. So in the application diectory, I have the Lib, Libs and DLLs folder for Python.

In the code, I have used Py_SetPythonHome() an Py_SetProgramName() to get Python loaded and also to allow standard libraries to be installed.

One of the test scripts I'm using has:

import csv

import numpy

The csv line is now fine. Within the \libs directory I can see site-packages\numpy. But the import crashes on this line. I am using numpy 1.6.1 for this.

I think I might need to change the module search path - is this right and what is the best way to achieve this to allow third-party libraries like numpy to be accessible to my scripts? You can assume that I could produce an absolute path to the numpy directory if that would help.

EDIT: More information - I've managed to produce the traceback and the error I'm getting is in \numpy\core\_init_.py when it tries the line "import multiarray" with the error "ImportError: DLL load failed: The specified module connot be found". Checking the directory, I find a multiarray.pyd. Any thoughts?

Fritz
  • 274
  • 3
  • 16
  • When you say 'the import crashes' do you mean it throws an ImportError exception? – deStrangis May 29 '12 at 16:54
  • I'm finding it difficult to work out what the error actually is - basically, I'm trying to import the script (which contains a class to be instantiated) using PyImport_Import. If I try to "import numpy" in the script, I get a null pointer as a result. If I comment that out, I get a non-null pointer – Fritz May 30 '12 at 08:12
  • @deStrangis: I've checked, and yes, it is an Import Error. I've got the numpy site-packages directory in the embedded interpreter's sys.path, but the import just fails... – Fritz Jun 01 '12 at 12:35
  • I'm having the exact same problem. Did you manage to solve this? – João Pereira Apr 17 '14 at 12:35
  • I'm having the exact same problem too. Did you manage to solve this? – bibi May 27 '15 at 20:12

2 Answers2

0

I have exactly the same problem with you, when I use python C API to import numpy. Some .pyd modules cannot be imported. When I changed to boost.python, there is no problem. Maybe you can try boost.python also. Here is sample:

Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
0

This turned out to be a DLL mismatch error. The numpy version that the code was looking had a slightly different compilation route to that of my C++ code that was embedding the interpreter.

The resolution was to recompile numpy against the Python distribution that I'd used in my application, but using exactly the same compiler settings. This cleared the problem.

Fritz
  • 274
  • 3
  • 16