Python-Module kann nicht importiert werden...

Python in C/C++ embedden, C-Module, ctypes, Cython, SWIG, SIP etc sind hier richtig.
Antworten
racnil
User
Beiträge: 3
Registriert: Sonntag 9. Januar 2011, 00:49

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?? :(
theliquidwave
User
Beiträge: 221
Registriert: Sonntag 1. Juni 2008, 09:08

Du musst schon den Scriptpath setzen. Ich mache das so:

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;
	}
Gruß
Grüßle.
racnil
User
Beiträge: 3
Registriert: Sonntag 9. Januar 2011, 00:49

Hallo Chrisber,
Danke für dein Antwort! :D :D

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"?
racnil
User
Beiträge: 3
Registriert: Sonntag 9. Januar 2011, 00:49

Oh, danke schön. Ich habe das gelöst. Einfach:
PyRun_SimpleString("import sys \n"
"sys.path.append(\".\") \n");
vor dem Importieren.
theliquidwave
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 :o
Als Ersatz kannst du sprintf verwenden.

Deine Lösung mag zwar funktionieren, ich betrachte sie aber als "dreckig".

Gruß
Grüßle.
Antworten