[C++-sig] Failed to import pyd File When python embed into C++

salinea liujz39 at gmail.com
Wed Jan 23 08:19:27 CET 2013


I create a pyd File named "testPyd" with boostPython,and then I import the
testPyd module into "test.py", it works perfect!
But when I embeded the python interpreter into my C++ project and run the
"test.py", it comes out a "ImportErr: no module named testPyd".
It has confused me for two days and I googled for long time,but I can't find
the answer!
Anybody here can help me ?
Thank you!

Here is my test examples: 
The C++ codes to build the *pyd File* is: 
Excute.h 

#ifndef EXCUTE_H_ 
#define EXCUTE_H_ 
#include <string> 

class Excute 
{ 
public: 
        Excute(){} 
        int getIoReuslt(std::string ioStr); 
        int getSignal(); 
}; 
#endif 

Excute.cpp: 
#include "Excute.h" 
#include "Explanation.h" 
#include <boost/python.hpp> 

int Excute::getIoReuslt(std::string ioStr) 
{ 
        return 1; 
} 

int Excute::getSignal() 
{ 
        int i = rand()%2; 
        return i; 
} 

BOOST_PYTHON_MODULE(pythonDll) 
{ 
        using namespace boost::python; 
        class_<Excute>("Excute", init<>()) 
                .def("getIoResult", &Excute::getIoReuslt) 
                .def("getSignal", &Excute::getSignal) 
                ; 
} 
Then a pyd File Named pythonDll is created(pythonDll.pyd), 
Here are the codes in my test.py: 
test.py: 

from pythonDll import* 
h=Excurte() 
print h.getIoResult("a") 
print h.getSignal() 

And this script works perfect in IDLE. 

And then I embed python into my C++ project,Here are the test codes: 
#include <Python.h> 
#include <string> 

int main(int argc, char* argv[]) 
{ 
        Py_Initialize(); 
        FILE * fp = fopen("$PATH/test.py", "r"); 
        if (fp == NULL) 
        { 
            return 1; 
        } 
        PyRun_SimpleString("execfile($PATH/test.py')"); 
        Py_Finalize(); 

        system("Pause"); 

        return 0; 
} 

The result is: 
Traceback (most recent call last): 
  File "<string>", line 1, in <module> 
  File "$PATH/test.py", line 1, in <module> 
    from pythonDll import* 
ImportError: No module named pythonDll 
[19228 refs] 



--
View this message in context: http://boost.2283326.n4.nabble.com/Failed-to-import-pyd-File-When-python-embed-into-C-tp4641675.html
Sent from the Python - c++-sig mailing list archive at Nabble.com.


More information about the Cplusplus-sig mailing list