Failed to import a "pyd: File When python intepreter embed in C++ project
liujz39 at gmail.com
liujz39 at gmail.com
Wed Jan 23 02:05:09 EST 2013
On Wednesday, January 23, 2013 12:49:43 PM UTC+8, Chris Angelico wrote:
>
>
> > Thank you!
>
>
>
> First thing you should do is to get a minimal example that
>
> demonstrates the problem, and *copy and paste* it and the exception
>
> traceback. When your own spelling is as sloppy as this, we need to see
>
> Python's accuracies to have any chance of being able to help you.
>
> Plus, the process of shrinking your example to the minimum often
>
> highlights the actual cause of the problem. Even if it doesn't, it'll
>
> certainly be easier for us to help you if it's a small, self-contained
>
> script than if it's huge.
>
>
>
> ChrisA
Thanks for your attention and sorry for the sloppy of my spelling!!!
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]
It bored me for a couple of days!If you get any ways to solve this ,please let me know!And thank you again!
More information about the Python-list
mailing list