<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Jan 23, 2013 at 8:19 AM, salinea <span dir="ltr"><<a href="mailto:liujz39@gmail.com" target="_blank">liujz39@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
I create a pyd File named "testPyd" with boostPython,and then I import the<br>
testPyd module into "test.py", it works perfect!<br>
But when I embeded the python interpreter into my C++ project and run the<br>
"test.py", it comes out a "ImportErr: no module named testPyd".<br>
It has confused me for two days and I googled for long time,but I can't find<br>
the answer!<br>
Anybody here can help me ?<br>
Thank you!<br>
<br>
Here is my test examples:<br>
The C++ codes to build the *pyd File* is:<br>
Excute.h<br>
<br>
#ifndef EXCUTE_H_<br>
#define EXCUTE_H_<br>
#include <string><br>
<br>
class Excute<br>
{<br>
public:<br>
Excute(){}<br>
int getIoReuslt(std::string ioStr);<br>
int getSignal();<br>
};<br>
#endif<br>
<br>
Excute.cpp:<br>
#include "Excute.h"<br>
#include "Explanation.h"<br>
#include <boost/python.hpp><br>
<br>
int Excute::getIoReuslt(std::string ioStr)<br>
{<br>
return 1;<br>
}<br>
<br>
int Excute::getSignal()<br>
{<br>
int i = rand()%2;<br>
return i;<br>
}<br>
<br>
BOOST_PYTHON_MODULE(pythonDll)<br>
{<br>
using namespace boost::python;<br>
class_<Excute>("Excute", init<>())<br>
.def("getIoResult", &Excute::getIoReuslt)<br>
.def("getSignal", &Excute::getSignal)<br>
;<br>
}<br>
Then a pyd File Named pythonDll is created(pythonDll.pyd),<br>
Here are the codes in my test.py:<br>
test.py:<br>
<br>
from pythonDll import*<br>
h=Excurte()<br>
print h.getIoResult("a")<br>
print h.getSignal()<br>
<br>
And this script works perfect in IDLE.<br>
<br>
And then I embed python into my C++ project,Here are the test codes:<br>
#include <Python.h><br>
#include <string><br>
<br>
int main(int argc, char* argv[])<br>
{<br>
Py_Initialize();<br>
FILE * fp = fopen("$PATH/test.py", "r");<br>
if (fp == NULL)<br>
{<br>
return 1;<br>
}<br>
PyRun_SimpleString("execfile($PATH/test.py')");<br>
Py_Finalize();<br>
<br>
system("Pause");<br>
<br>
return 0;<br>
}<br>
<br>
The result is:<br>
Traceback (most recent call last):<br>
File "<string>", line 1, in <module><br>
File "$PATH/test.py", line 1, in <module><br>
from pythonDll import*<br>
ImportError: No module named pythonDll<br>
[19228 refs]<br>
<br></blockquote><div><br>Just to help you debug the issue: have you tried putting something like this into your test.py?<br><br>import sys<br>sys.stderr.write("PYTHONPATH = %s\n"%(":".join(sys.path)))<br>
</div></div><br><br>Michael<br></div></div>