Ondrej Certik | 21 Jan 19:47 2011
Picon

how to use Cython to use Python from C++

Hi,

previously my approach to this was to use Cython "api" directive and
compile a .pyx into an .so, and then import it from C++ using
import_my_module() api call, that Cython exports.

This has one big problem, in fact a show stopper problem, that one
needs to know the PYTHONPATH to this .so module at runtime, and I have
not figured out how to solve it, thus causing quite a rejection of
using Python from C++ in our group. In practice, people were
constantly reporting, that the import of the .so fails (module not
found).

Couple days ago, I have figured out an excellent solution. So I wanted
to share it with you, if there are any potential pitfalls. I compile
the generated .cpp file into the C++ application directly, thus there
is no .so generated. Then, in the application, I initialize Python
using C Python API, and call the "initmy_module()" function, that is
always present in the genreated .cpp file. And then I simply import it
in Python (that I execute using Python C/API calls) by "from my_module
import some_stuff". In fact, in practice, I don't call any Python C /
API directly, I use Cython for everything. Here is the code:

https://github.com/hpfem/hermes/blob/master/hermes_common/python/python_api.cpp#L26

I am copying it here:

void Python::_init(int argc, char* argv[])
{
    python_count++;
(Continue reading)

Lisandro Dalcin | 1 Feb 22:31 2011
Picon

Re: how to use Cython to use Python from C++

On 21 January 2011 15:47, Ondrej Certik <ondrej <at> certik.cz> wrote:
> Hi,
>
> previously my approach to this was to use Cython "api" directive and
> compile a .pyx into an .so, and then import it from C++ using
> import_my_module() api call, that Cython exports.
>
> This has one big problem, in fact a show stopper problem, that one
> needs to know the PYTHONPATH to this .so module at runtime, and I have
> not figured out how to solve it, thus causing quite a rejection of
> using Python from C++ in our group. In practice, people were
> constantly reporting, that the import of the .so fails (module not
> found).
>
> Couple days ago, I have figured out an excellent solution. So I wanted
> to share it with you, if there are any potential pitfalls. I compile
> the generated .cpp file into the C++ application directly, thus there
> is no .so generated.

Of course.

> Then, in the application, I initialize Python
> using C Python API, and call the "initmy_module()" function, that is
> always present in the genreated .cpp file. And then I simply import it
> in Python (that I execute using Python C/API calls) by "from my_module
> import some_stuff". In fact, in practice, I don't call any Python C /
> API directly, I use Cython for everything. Here is the code:
>
> https://github.com/hpfem/hermes/blob/master/hermes_common/python/python_api.cpp#L26
>
(Continue reading)

Alex van Houten | 9 Nov 13:37 2011
Picon

Re: how to use Cython to use Python from C++

Ondrej Certik <ondrej <at> certik.cz> writes:

>
https://github.com/hpfem/hermes/blob/master/hermes_common
>/python/python_api.cpp#L26
> 
> and here is the  python_engine.pyx:
> 
> https://github.com/hpfem/hermes/blob/master/hermes_common/python> 
> /python_engine.pyx
>  
> https://github.com/hpfem/hermes/blob/master/hermes2d/src/mesh
> /python_reader.pyx
> 
> Full details are here:
> 
> https://github.com/hpfem/hermes/blob/master/hermes_common
> /python/python_api.h
> 
These urls seem not to be correct at this time.

Alex.

Alex van Houten | 15 Nov 17:19 2011
Picon

Re: how to use Cython to use Python from C++

Ondrej Certik <ondrej <at> certik.cz> writes:

> previously my approach to this was to use Cython "api" directive and
> compile a .pyx into an .so, and then import it from C++ using
> import_my_module() api call, that Cython exports.

Could you please show me a minimal example of how this works?
I am having a hard time to get this running without segfaulting.

Thanks,
Alex.


Gmane