Python class in c++ using the cython? [duplicate] - python

I'm working in an embedded Linux environment and I have some Python code which I would like to use. My Python code is just doing some math, not using any library other than Numpy and the common ones.
Is there any way to build up a library that I can call from C or C++ code?

Embedding the CPython interpreter into a C or C++ program is actually pretty straightforward.
The official documentation has some complete examples.
Also, check out SWIG and Boost.Python.

Related

How to communicate between Python and other languages?

I want to make a small application with graphical interface with Python but I want to do the calculations in another language such as C++ or Golang, is it possible ?
Build a .dll or .so file from your C++ source, then use Python's ctypes to import the functions from it. Alternatively, use the Python C API to create an extension module. The difference is that in the first approach, the interop code will be in Python, and in the second, it will be in your native code.

Create dynamic library (.so file) from Python code

Suppose I have a library written in Python that I'd like to call from a C software. Is there any way I can compile this Python code to a .so file?
This should help.
Also you can do both ways i.e. extend python by writing library in C and call it from Python code OR code functions in C that could be called from Python.
The standard term used (conceptually and help googling is "Python bindings"
Official reference from python docs
General reference - Integrating python with other languages

Use Python code in C/C++

I'm working in an embedded Linux environment and I have some Python code which I would like to use. My Python code is just doing some math, not using any library other than Numpy and the common ones.
Is there any way to build up a library that I can call from C or C++ code?
Embedding the CPython interpreter into a C or C++ program is actually pretty straightforward.
The official documentation has some complete examples.
Also, check out SWIG and Boost.Python.

ctypes - does python need to be compiled with same version as C library?

I'm running into some really weird problems with ctypes. I'm using ctypes to interface to a C++ library that has a C interface. The library has lots of parallel functionality. More often than not, the parallel calls tend to end up with a segfault from the C++ layer, but I've run into them with some serial code too. I'm wondering if there is any restriction on whether the Python interpreter and the C++ code need to be compiled with the same version of the C++ compiler? If so, how do I find out what c++ was used to build Python? I've tried to run strings on Python and grep for gcc and g++, nothing shows up.
There's no requirement at all that the native library that you call with ctypes has to be built with a matching runtime. In fact there's not even a requirement that the native library even uses a C runtime.
For example you can use ctypes to call code written in other languages, e.g. Delphi. Or you can use ctypes to call Windows API functions which are not linked against MSVC.
I guess you'll have to look elsewhere to resolve your problem!
CPython doesn't contain C++ code and thus there is no requirement with regards to the C++ compiler used to compile a C++ library loaded into a CPython process. However, the C layer and the C++ library must generally be compiled with the same C++ compiler. And if the C++ library or the C layer link against libpython, they must be compiled against the same version of CPython that is later used to run the ctypes code that loads the library.

using cython to port modules to python 3.1

Is it possible to import arbitrary modules in cython, compile them to shared object files and then use them in python 3.1?
The reason for this is, that I am writing an extension for the program "blender", which has an internal python 3.1 interpreter. But i would also like to make use of some python-modules which are not ported to 3.x, yet
I have specifically numpy in my mind (but also some other libraries). I have a module, which makes use of numpy. As I want to redistribute that module, I don't want poeple to install numpy on their machines. would that work?
In principle, I believe it's possible. Cython works by translating Python-like code to C code. That code can be compiled for either Python 2 or Python 3 (it uses C preprocessor statements to change which code is used).
The bad news is that it will only work for extensions written in Python-like code that Cython can translate. You can't use Cython on extensions written in C, like Numpy.
The good news is that, at least for Numpy, you shouldn't have to. Since version 1.5, Numpy supports Python 3. There's a binary available for Windows; on other systems, you might have to work out how to compile the code yourself.
Check your Python documentation, the section "Python/C API Reference Manual" describes in detail how to do it.
EDITED:
So what you want is porting a 2.x lib to 3.x. That's a big work to do.
If the solution your wished exists. Python 2.x should have been eliminated by now.
Sure there is a 2to3 tool. But a Python lib written by C is not applicable in this way.
So, you may follow the instruction in the "Python/C API Reference Manual" to port the lib to 3.x or just wait.

Categories