C++ Funktionen in Python aufrufen

Python in C/C++ embedden, C-Module, ctypes, Cython, SWIG, SIP etc sind hier richtig.
Antworten
Helgefan
User
Beiträge: 2
Registriert: Mittwoch 14. Juli 2010, 14:52

Hallo zusammen!
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;
}
Pythoncode test.py:

Code: Alles auswählen

#!/usr/bin/env python
print "Hallo"
import impo    # schlägt schon fehl
print impo.huiui("sodenn")
Kompilieren tu ich den C++-Code in Linux (RedHat 5, 64 bit) mit folgendem Makefile (erfolgreich!):

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)
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:

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
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
Benutzeravatar
snafu
User
Beiträge: 6738
Registriert: Donnerstag 21. Februar 2008, 17:31
Wohnort: Gelsenkirchen

Vermutlich ist Boost.Python was für dich. Hier gibt es ein Hello-World Beispiel dazu. Im Übrigen würde ich dringend mal upgraden. Python ist aktuell bei Version 2.7, wobei die zugegeben erst 1-2 Wochen alt ist. Du solltest aber, wenn möglich, zumindest 2.6 nutzen.
tordmor
User
Beiträge: 100
Registriert: Donnerstag 20. November 2008, 10:29
Wohnort: Stuttgart

Du hast embedding python und extending python verwechselt. Ein embedded python ist ein Programm mit einer main methode, dass irgendwann python startet. D.h. Du startest in dem Fall nicht python oder test.py sondern das Programm, das dann von sich aus python startet und test.py lädt und ausführen lässt.

Wenn Du python erweiterst mit einem C-/C++-Modul hat das Modul keine eigene main Funktion, sondern wird eine shared library (.so auf Linux/Mac, .dll auf windows) und muss entsprechend mit -shared compiliert werden.
http://www.felix-benner.com
Helgefan
User
Beiträge: 2
Registriert: Mittwoch 14. Juli 2010, 14:52

Ah das macht natürlich Sinn :roll:
Werds gleich mal ausprobieren, danke schonmal!

Bis denn,
Helgefan
Antworten