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

Wojciech Mamrak wmamrak at gmail.com
Wed Jan 23 12:16:37 CET 2013


Before initilizing Python interpreter you need to register your module:
PyImport_AppendInittab("pythonDll", initpythonDll);

regards

2013/1/23 Michael Wild <themiwi at users.sourceforge.net>:
> On Wed, Jan 23, 2013 at 8:19 AM, salinea <liujz39 at gmail.com> wrote:
>>
>>
>> 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]
>>
>
> Just to help you debug the issue: have you tried putting something like this
> into your test.py?
>
> import sys
> sys.stderr.write("PYTHONPATH = %s\n"%(":".join(sys.path)))
>
>
> Michael
>
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig


More information about the Cplusplus-sig mailing list