C++ Funktionen in Python aufrufen
Verfasst: Mittwoch 14. Juli 2010, 15:14
Hallo zusammen!
Ich versuche gerade, eine simple C++ Funktion "huiui" mit Python aufzurufen. Mein C-Code sieht so aus:
impomodule.c:
Pythoncode test.py:
Kompilieren tu ich den C++-Code in Linux (RedHat 5, 64 bit) mit folgendem Makefile (erfolgreich!):
Ja, ich weiß das Installationsverzeichnis von Python ist schon komisch, aber das sollte eigentlich kein Problem sein (und auf das Standardverzeichnis hab ich sowieso keine Zugriffsrechte).
Wenn ich jetzt also "test.py" aufrufe, kommt folgende Fehlermeldung:
Das "'import site' failed; use -v for traceback" kommt komischerweise bei jedem Pythonprogramm, obwohl die ansonsten trotzdem normal laufen.
Hat jemand ne Idee, warum der mein impo Modul nicht finden kann?
Grüße,
Helgefan
Ich versuche gerade, eine simple C++ Funktion "huiui" mit Python aufzurufen. Mein C-Code sieht so aus:
impomodule.c:
Code: Alles auswählen
#include <Python.h>
#include <iostream>
static PyObject *impo_huiui(PyObject *self, PyObject *args){
int sts;
if (!PyArg_ParseTuple(args, "i", &sts))
return NULL;
std::cout << "TESTEST";
return Py_BuildValue("i", 2*sts);
}
static PyMethodDef ImpoMethods[] = {
/*...*/
{"huiui", impo_huiui, METH_VARARGS,
"Execute a shell command."},
/*...*/
{NULL, NULL, 0, NULL}
};
static PyObject *ImpoError;
PyMODINIT_FUNC initimpo(void)
{
PyObject *m;
m = Py_InitModule("impo", ImpoMethods);
if (m == NULL)
return;
ImpoError = PyErr_NewException("impo.error", NULL, NULL);
Py_INCREF(ImpoError);
PyModule_AddObject(m, "error", ImpoError);
}
int main(int argc, char *argv[])
{
/* Pass argv[0] to the Python interpreter */
Py_SetProgramName(argv[0]);
/* Initialize the Python interpreter. Required. */
Py_Initialize();
/* Add a static module */
initimpo();
return 0;
}
Code: Alles auswählen
#!/usr/bin/env python
print "Hallo"
import impo # schlägt schon fehl
print impo.huiui("sodenn")
Code: Alles auswählen
TARGET=impomodule
CC=g++
CFLAGS=-c
SOURCES=impomodule.c
OBJECTS=$(SOURCES:.c=.o)
INCS = -I/proj/F/FANBAU/Salome-5.1.4/Python-2.4.4/include/python2.4
LIBS = -L/proj/F/FANBAU/Salome-5.1.4/Python-2.4.4/lib -lpython2.4
all: $(TARGET)
$(TARGET): $(OBJECTS)
$(CC) -o $(TARGET) $(LIBS) $(OBJECTS)
impomodule.o: $(SOURCES)
$(CC) $(CFLAGS) $(INCS) $(SOURCES)
Wenn ich jetzt also "test.py" aufrufe, kommt folgende Fehlermeldung:
Code: Alles auswählen
'import site' failed; use -v for traceback
Hallo
Traceback (most recent call last):
File "./test.py", line 3, in ?
import impo
ImportError: No module named impo
Hat jemand ne Idee, warum der mein impo Modul nicht finden kann?

Grüße,
Helgefan