[C++-sig] Exposing classes

Pierre Barbier de Reuille pierre.barbier at cirad.fr
Wed Jul 27 16:14:50 CEST 2005

Ok, the problem is not about how you export your C++ functions, it's how
you import the module !

When you use the Python "import" statement, Python will look for a file
! And that you don't want as your module is inside your program (and not
as a separate file). So you need to use the C API of Python to import
it. The function is called PyImport_AppendInittab.

Another possibility is to create a DLL with the Boost.Python code (and
not the actual C++ code ...) so there is a hello.dll or hell.pyd file to
load when you use the import statement. However, I really don't know how
well it will work on Windows !


Ira Holtzer a écrit :
> OK, I'm new at this so please bear with me.
> First off, I'm using boost::python so what low level functions do you mean
> (can you give me one name so I know where to start looking)?
> Second, what I did is the following:
> Created an exe file (SimplePython.exe ) what has a standard greet() function
> and the boost::python declaration:
> {
>     def("greet", greet);
> }
> I run the exe which feeds the interpreter with the script (via
> PyRun_String):
> import hello
> print hello.greet()
> To which the interpreter returns an exception saying "ImportError: No module
> named hello.
> Can this be a path problem, do I somehow need to specify a dll/exe that has
> c++ exported symbols?
> Thanks,
> Ira
> ----- Original Message ----- 
> From: "Pierre Barbier de Reuille" <pierre.barbier at cirad.fr>
> Newsgroups: gmane.comp.python.c++
> Sent: Wednesday, July 27, 2005 10:02 AM
> Subject: Re: Exposing classes
> Well, don't know for windows, but for Linux if your symbols are defined
> inside your executable file, it works well ! Even your whole module can
> be defined in your executable file. You just have to initialise it from
> the C API with the low level functions (i.e. the ones who won't try to
> look for a file named after your module name ...). Also, once your
> module is loaded, every other "normal" import will just work !
> Pierre
> Ira a écrit :
>>A question on basics...
>>Do all my exposed C++ symbols need to be in .dll format (win32 of course)?
>>What I mean is, can I have an exe running a python intrpreter where
> exposed
>>symbols are compiled *inside* the exe and somehow be able to access them
>>from the python scripts?
>>How would I import the modules in such a case?
>>C++-sig mailing list
>>C++-sig at python.org

Pierre Barbier de Reuille

INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde

tel   : (33) 4 67 61 65 77    fax   : (33) 4 67 61 56 68

More information about the Cplusplus-sig mailing list