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

Unable to call custom module using Python/C API

File structure: Foo/ list.so main.cpp list.cpp boost_wrapper.cpp main.cpp code: #include #include "list.cpp" int main(int argc, char *argv[]){ PyObject *pimport; pimport=PyString_FromString("list"); …
Nihal Harish
  • 980
  • 5
  • 13
  • 31
1
vote
0 answers

How do I pass a populated c structure to python (without ctypes)

How can I pass a c structure to python without using ctypes ? I have a pointer to a stats_res_t structure that I'm trying to return to python. I'm trying something along the lines of PyObject* pyobject_get_system_stats(PyObject *self, PyObject…
nkshirsa
  • 11
  • 3
1
vote
1 answer

Python C API Read a list of list, allocate memory and global Variables

I am writing a module in order to optimize an operation but I have a few questions. First I need to read an array (list of list) from python to C, I've done that this way: long i_k; for (int k = 0; k < n; k++) { PyObject *first =…
Alejandro Alcalde
  • 5,990
  • 6
  • 39
  • 79
1
vote
2 answers

Embedding Python into C example not working

I'm following this tutorial on Embedding Python on C, but their Pure Embedding example is not working for me. I have on the same folder (taken from the example): call.c #include int main(int argc, char *argv[]) { PyObject *pName,…
Diego Herranz
  • 2,857
  • 2
  • 19
  • 34
1
vote
1 answer

PyArg_ParseTuple() on arbitrary tuples

I am looking for confirmation on this issue: Can I use PyArg_ParseTuple() on any Python tuple, or just on those passed as argument lists from function calls? I see strong indication for the former, but to my reading the documentation is rather vague…
TFM
  • 717
  • 6
  • 21
1
vote
2 answers

Unhandled exception at multiarray.pyd the 2nd time the program runs

I'm making a .dll plug-in in c++ and embedding python 2.7 in it. Everything worked fine with simple .py programs until I imported a large program. The strangest thing is that the program runs with no problem the first time, but the second time an…
João Pereira
  • 673
  • 1
  • 9
  • 29
1
vote
1 answer

Trouble with modules while embedding python in C++

I'm making a .dll for an .exe program and embedding python in it. It worked fine with this simple .py program from time import * ##import OptimRestriction def test_callsign(b): ...(simple script) return What I did was copy the .py…
João Pereira
  • 673
  • 1
  • 9
  • 29
1
vote
1 answer

How to convert the numpy.ndarray to a cv::Mat using Python/C API?

I use python as an interface to operate the image, but when I need to write some custom functions to operate the matrix, I find out that numpy.ndarray is too slow when I iterate. I want to transfer the array to cv::Mat so that I can handle it easily…
Dawei Yang
  • 606
  • 1
  • 9
  • 19
1
vote
0 answers

Python C API: initialize instance member objects to NULL or None?

When creating a new instance of a custom class in the C API, is it preferable to initialize the member variables to NULL or None?
more tension
  • 3,312
  • 17
  • 19
1
vote
0 answers

Embed Python: Set path to executable

I would like to embed Python (in a Cocoa App under Mac OS X Mavericks). I have several Pythons installed and would like to set the one used in the App specifically. How do I do that? I tried it via Py_SetProgramName("path/to/python") but when I…
DaPhil
  • 1,509
  • 4
  • 25
  • 47
1
vote
1 answer

Some confustion about PyClass_Check and PyClass_IsSubclass functions

I have occurred problems when I learn python embeded C programming. Here's my sample: ReadBuf.c #include "Python.h" static PyObject* Test_IsInstance(PyObject* self, PyObject* args){ PyObject* pTest = NULL; PyObject* pName = NULL; …
Vatel
  • 79
  • 7
1
vote
1 answer

Pass command line arguments to python 2.7.6 package application using C API

I'm new to python and now I need to call a python 2.7.6 program using its C API. The python program is in the form of a python package and takes several command line options. You can run it like this: python my_py_app input.txt --option1="value1"…
Jay Zhao
  • 946
  • 10
  • 24
1
vote
1 answer

Python C/API assign a C++ variable

I am writing a small program with the Python C/API, which basically calls a simple Python script. Here's the code: #include PyObject *pName, *pModule, *pDict, *pFunc; int main() { Py_Initialize(); pName =…
jndok
  • 909
  • 3
  • 14
  • 28
1
vote
5 answers

Extending Python: pre-load my C module

I'm trying to extend Python interpreter by a few C functions I wrote. From reading docs, to expose those function the user has to import the module encompassing the functions. Is it possible to load pre-load or pre-import via C API the module so…
EcirH
  • 473
  • 1
  • 5
  • 9
1
vote
1 answer

Missing python 3 API functions

I was coding something at work and it seems that some C API functions provided by python are not working. I tried mainly the function that check types, for example: import ctypes python33_dll = ctypes.CDLL('python33.dll') a_float =…
Mac
  • 3,397
  • 3
  • 33
  • 58