Questions tagged [python-c-api]

API used by C and C++ programmers who want to write extension modules or embed Python.

The Application Programmer’s Interface to Python gives C and C++ programmers access to the Python interpreter at a variety of levels. The API is equally usable from C++, but for brevity it is generally referred to as the Python/C API. There are two fundamentally different reasons for using the Python/C API. The first reason is to write extension modules for specific purposes; these are C modules that extend the Python interpreter. This is probably the most common use. The second reason is to use Python as a component in a larger application; this technique is generally referred to as embedding Python in an application.

Writing an extension module is a relatively well-understood process, where a “cookbook” approach works well. There are several tools that automate the process to some extent. While people have embedded Python in other applications since its early existence, the process of embedding Python is less straightforward than writing an extension.

Many API functions are useful independent of whether you’re embedding or extending Python; moreover, most applications that embed Python will need to provide a custom extension as well, so it’s probably a good idea to become familiar with writing an extension before attempting to embed Python in a real application.

Reference: http://docs.python.org/c-api/intro.html

1096 questions
1
vote
1 answer

Unpacking iterable in Python extension

I'm trying to translate a, b, c, d = iterable to Python's C/API. I'm looking for a function similar to PyArg_ParseTuple, just for iterables. In other words, something in the area of PyIter_Parse or PyObject_ParseIterable, if such functions would…
Jessie K
  • 93
  • 7
1
vote
1 answer

Redirect Embedded Python IO to a console created with AllocConsole Win32 application

i know that there is similar question but my efforts to resolve this problem were not successful. I want to redirect Python interpreter I/O, but i have only succeeded to redirect stdout. I have still problem with stdin and stderr. Based on Redirect…
1
vote
1 answer

Argc and argv for external program calling C shared library

I'm trying to use Maple to call a C shared library which calls Python. Usually I need to set argc and argv in main, but since this is another program (Maple) calling the shared library, I don't have the main function (or should I have one?). Then…
snsunx
  • 155
  • 1
  • 9
1
vote
0 answers

How to use python 2.6 from Visual Basic 2005?

What's the best way to call python scripts from Visual Basic 2005? I've got an application written in visual basic 2005 that needs to call into a library written in python. The library requires python 2.6. I'm using the python C API to access the…
Darryl
  • 5,907
  • 1
  • 25
  • 36
1
vote
0 answers

Using Tensorflow in C++ with Python/C API results in missing default graph

I want to train a Tensorflow model in C++ using the Python/C API (I am aware of the C++ API of Tensorflow, but it's too restricted). First I create the model in Python and then I export it. Ater that I reload it in C++. The problem: Unfortunately…
swunsch
  • 11
  • 1
1
vote
0 answers

How to Call lambdify (of Sympy) from C++?

I am trying to call Sympy's lambdify from C++ code using Python/C API. The code is pasted below. const char * file = "sympy"; const char * function = "lambdify"; Py_Initialize(); PyObject* pModuleString = PyString_FromString(file); PyObject*…
1
vote
1 answer

How to get the pointer of a returned PyObject in Python C API?

I'm trying to call a Python function in C using Python C API. I use PyObject_Call to call the Python function which returns me a PyObject. If the returned PyObject is a numpy array or a list then how should I handle it? Basically if I want to cast…
snsunx
  • 155
  • 1
  • 9
1
vote
1 answer

Python C API segmentation fault

I was trying to pass an array from C to python and perform a simple arithmetic operation numpy.prod, but encountered a segmentation error. Could anybody help me point out where went wrong? Thank you! #include #include #define…
snsunx
  • 155
  • 1
  • 9
1
vote
1 answer

Including external shared intel's mkl library in c extension for python

I wrote a python c extension that uses routine from Intel's math kernel library (mkl). This is the first time that I write a c extension. I just learned that today. The c extension compiled. But when I import it in python, it says undefined symbol,…
rxu
  • 1,369
  • 1
  • 11
  • 29
1
vote
0 answers

C extension with integer & numpy array as argument

I want to create a c extension to python that takes one integer, and two numpy array as argument. How to do that? Thank you. This works: #include "Python.h" #include "numpy/arrayobject.h" static PyObject* test4 (PyObject *self, PyObject *args) { …
rxu
  • 1,369
  • 1
  • 11
  • 29
1
vote
0 answers

numpy objectarray.h compile error with MingW64

I'm trying to compile python cpp library using numpy arrayobject.h on Windows 7 x64. It keeps showing errors. Could anybody help me out to fix them? The error is like following. ndarrayobject.h:139:52: error: cannot convert 'int*' to 'npy_intp* {aka…
fx-kirin
  • 1,906
  • 1
  • 20
  • 33
1
vote
0 answers

implementing loading bar in python C extension (Python C API)

I would like to display the current state of my C++ python extension (.pyd module) in the python interpreter window as a loading bar, like pip or conda does when it is telling you the current download progress. In other words, I would like to write…
JoseOrtiz3
  • 1,785
  • 17
  • 28
1
vote
0 answers

C's Python API: Calling python function 2X causes Access violation writing location

I'm trying to write some C code that calls some python code to do some data analysis, and I came upon a weird issue. If I call initialize python, call a function, finalize python, and then repeat the same 1 time, I get an access violation writing…
aquirdturtle
  • 2,038
  • 26
  • 20
1
vote
2 answers

Call python in parallel from C

I need to call a Python function from my C code. It works perfectly, but when I want to do parallelization, it breaks down. Please see the following minimal C code: #include #include int main(void) { double Z = 1.; double k…
Hongcheng Ni
  • 427
  • 1
  • 4
  • 11
1
vote
1 answer

python3 str object cannot pass PyUnicode_Check

I was writing a C extension function, which was supposed to accept a str object as argument. The code is shown below: static PyObject *py_print_chars(PyObject *self, PyObject *o) { PyObject *bytes; char *s; if (!PyUnicode_Check(o)) { …
sun
  • 95
  • 1
  • 7