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
0
votes
1 answer

Python3 C-API: PyObject attributes not exist after passing it to another python function

I am trying to embed a python code as part of my cpp code. So, I impelement the following class to call RunModel in a loop after calling LoadModel: class PyAdapter { private: PyObject *pModule; PyObject *pModel; PyObject…
ma.mehralian
  • 1,274
  • 2
  • 13
  • 29
0
votes
1 answer

How to catch Python 3 stdout in C++ code

In an old question about how to catch python stdout in C++ code, there is a good answer and it works - but only in Python 2. I would like to use something like that with Python 3. Anyone could help me here? UPDATE The code I am using is below. It…
0
votes
1 answer

Using C application with a Python GUI

I have built a simple application in C and I want it to have a GUI. I want the GUI to be in Python (since it's easier) but am not sure how I would get the user response from Python to C or even if it's possible. What I want is a tkinter window to…
Xantium
  • 11,201
  • 10
  • 62
  • 89
0
votes
1 answer

Unable to import Python module written in C

I have been trying to work out how to make a .pyd (Python Extension Module) file from a C script (without swig or anything else except MinGW) and have successfully built it into a .pyd. The problem however occurs when I try and import the module. If…
Xantium
  • 11,201
  • 10
  • 62
  • 89
0
votes
2 answers

initializer is not a constant, error C2099, on compiling a module written in c for python

i tried to compile a python module called distance, whith c "python setup.py install --with-c" using msvc 2017 on windows 10, i got this error , Cdistance / distance.c (647): error C2099: initializer is not a constant Cdistance / distance.c…
Slimane amiar
  • 934
  • 12
  • 27
0
votes
1 answer

Calling Python-3 function in C

I am trying to call Python code in C with this code: #include #include int main() { PyObject* pInt; Py_Initialize(); PyRun_SimpleString("print('This is Python in C')"); Py_Finalize(); printf("\n"); …
Xantium
  • 11,201
  • 10
  • 62
  • 89
0
votes
1 answer

Is it necessary to destuct a c-class in the python-c-api destuction function?

I have defined a python-c-api class, its real data structure is as follow: typedef struct _C_CMISS_h { PyObject_HEAD // == PyObject ob_base; TestClass _in_Handle; }C_CMISS_h; Here is the destruction function, every time the…
cainmagi
  • 21
  • 1
  • 5
0
votes
0 answers

Mysterious TypeError using Python and NumPy C-API

I am writing a Python script, extended with C for computationally intensive portions of code. Most of the data passed between Python and C is contained in NumPy arrays. These arrays are attributes of some object dataobject. In addition, some…
aa-g
  • 1
  • 1
0
votes
1 answer

Valgrind error and memory leaks with Python/C API

I'm actually developping a game in C++ and trying to do the AI with a script langage. To do so, i choosed Python2 with Python/C api. My AI is actually working but there is a big problem : when I run valgrind on my program, there is a lot of error…
Kulbuto
  • 79
  • 1
  • 13
0
votes
0 answers

C Python arbitrary argument

in plain python (2.7) one can write variadic function like so: >> def myfunc(a, b, *args): print a,b,args >> params = (1,2,"3", "abc") >> myfunc(*params) # Will print "1 2 ('3', 'abc')" >> myfunc(params) # Will fail with 'myfunc() takes at…
Perfect99
  • 9
  • 2
0
votes
0 answers

Accelerate python program using python-C-API and POXIS threads?

I wrote some program in python, which runs steps similar to, but more complicate than following steps: STEP 1: Given a BATCH of lists with same length, each element of one list represents the number of states it may have, I need to DFS all the…
killa
  • 123
  • 6
0
votes
0 answers

Why does PyImport_ImportModule return a NULL object type and not nullptr?

I am trying to convert code that was written using PyRun_SimpleString to code using the C API more robustly. I've noticed some unusual behaviour in my code. 1) Py_GetVersion() returns a different version than PyObject_GetAttrString(sys, "version")…
Chris
  • 301
  • 2
  • 11
0
votes
2 answers

tensorflow::Tensor to python Tensor or numpy.nd_array

I have a C++ tensorflow::Tensor whose contents I am trying to access in Python. I have looked through the Tensorflow C++ Documentation in search of a function that can convert a tensorflow::Tensor to any sensible PyObject (it does not matter for now…
JoSauderGH
  • 337
  • 4
  • 15
0
votes
1 answer

Execute Python function provided in string in c++

I'm trying to create a Python Function from a string and executing it. I just cannot figure it out how to execute it properly. The code I wrote doesn't return the value I would expect, but a Object of Type _PY_NoneStruct. string code = R"( def…
0
votes
1 answer

when I build a PyObject with C API of Python3, its ob_type is NULL

I ran such code in C++11: PyObject* aa = PyLong_FromLong(7L); Then I checked the value of aa, it's not NULL, but aa->ob_type was NULL. However, when I ran: PyObject* aa = PyLong_FromLong(257L); aa->ob_type was not NULL anymore. I read the document…
killa
  • 123
  • 6