Hallo Leute,
ich benutze OpenSUSE 11.3 und möchte Python in C++ embedden. Ich habe folgende Sourcecode:
=======================================================
//C++ file:
#include <python/Python.h>
#include <iostream>
using namespace std;
int main()
{
PyObject *pModule, *pFunc;
PyObject *pArgs, *pValue;
Py_Initialize();
pModule = PyImport_ImportModule("module");
if(!pModule)
{
cout<<"Cannot import module"<<endl;
return 1;
}
Py_DECREF(pModule);
Py_Finalize();
return 0;
}
=======================================================
#Python File:
#Filename: module.py
addNum=0
def add(a):
print "in function add..."
global addNum
addNum=addNum+a
def result():
print "in functiopn result..."
global addNum
print addNum
=======================================================
Das Kompilieren ist erfolgreich, aber wenn das Programm läuft, scheint es immer:
Cannot import module
Das heißt pModule == NULL, aber warum??
Python-Module kann nicht importiert werden...
-
- User
- Beiträge: 221
- Registriert: Sonntag 1. Juni 2008, 09:08
Du musst schon den Scriptpath setzen. Ich mache das so:
Gruß
Code: Alles auswählen
PyObject *pyPath = PyList_New(5);
if (!pyPath)
{
pUtils->Print("[PSS] Failed to set home path for script '%s'\n", this->m_pScript->GetRealName().c_str());
return false;
}
PyList_SetItem(pyPath, 0, PyString_FromString(pUtils->BuildPath("%s/scripts/%s", pUtils->GetPSSPath().c_str(), this->m_pScript->GetName().c_str()).c_str()));
PyList_SetItem(pyPath, 1, PyString_FromString(pUtils->CombinePath(pUtils->GetPSSPath(), "libraries").c_str()));
PyList_SetItem(pyPath, 2, PyString_FromString(pUtils->CombinePath(pUtils->GetPSSPath(), "shared").c_str()));
PyList_SetItem(pyPath, 3, PyString_FromString(pUtils->CombinePath(pUtils->GetPSSPath(), "python").c_str()));
#ifdef WIN32
PyList_SetItem(pyPath, 4, PyString_FromString(pUtils->CombinePath(pUtils->GetPSSPath(), "python/plat-win").c_str()));
#else
PyList_SetItem(pyPath, 4, PyString_FromString(pUtils->CombinePath(pUtils->GetPSSPath(), "python/plat-linux2").c_str()));
#endif
if (PySys_SetObject("path", pyPath))
{
pUtils->Print("[PSS] Failed to set home path for script '%s'\n", this->m_pScript->GetRealName().c_str());
Py_DECREF(pyPath);
return false;
}
Grüßle.
Hallo Chrisber,
Danke für dein Antwort!
Aber, beim Kompilieren gibt's Fehler:
"pUtils was not declared in this scope"
Trotzdem finde ich, wenn ich "os" module importiere, dann funktioniert es. Das heißt, Scriptpath fehlt es doch. Aber, wo kommt "pUtils"?
Danke für dein Antwort!
Aber, beim Kompilieren gibt's Fehler:
"pUtils was not declared in this scope"
Trotzdem finde ich, wenn ich "os" module importiere, dann funktioniert es. Das heißt, Scriptpath fehlt es doch. Aber, wo kommt "pUtils"?
-
- User
- Beiträge: 221
- Registriert: Sonntag 1. Juni 2008, 09:08
pUtils ist eine Klasse von mir, dass du so etwas nicht bemerkst wundert mich doch etwas
Als Ersatz kannst du sprintf verwenden.
Deine Lösung mag zwar funktionieren, ich betrachte sie aber als "dreckig".
Gruß
Als Ersatz kannst du sprintf verwenden.
Deine Lösung mag zwar funktionieren, ich betrachte sie aber als "dreckig".
Gruß
Grüßle.