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

Python C API call to error() binds to libc implementation instead of a local one

EDITTED See at end of post for edit in response to Employed Russian's comment Disclaimer  Before going forward, I know that naming a function error is generally bad practice since it may clash with a similar function in libc, but this is an issue I…
Tachikoma
  • 179
  • 2
  • 9
2
votes
2 answers

creating a 3d numpy array from a non-contigous set of contigous 2d slices

Is it possible to use PyArray_NewFromDescr to create numpy array object from a set of contiguous 2d arrays, without copying the data?
Magnus
  • 643
  • 8
  • 15
2
votes
1 answer

Import functions from dll (programmed in C++) into Python script when the dll has an embedded Python interpreter

I wrote a library (dll) in c++ that uses an embedded Python interpreter. Executing Python scripts in the embedded interpreter works fine when the library is used by a c++ program. If I call the functions of the dll from a Python program I run into…
siehe-falz
  • 469
  • 3
  • 8
2
votes
0 answers

Equivalent to __all__ in Python C API

I am writing a simple extension module using the C API. Here is a test example: #include #include typedef struct { PyObject_HEAD int i; } MyObject; static PyMemberDef my_members[] = { {"i", T_INT,…
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
2
votes
1 answer

Python ctypes: how to pass row outputs from a C function into a pandas DataFrame?

My question is how to parse tab-delimited output from a C function into a pandas DataFrame via ctypes: I am writing a Python wrapper in Python3.x around a C library using ctypes. The C library currently does database queries. The C function I am…
ShanZhengYang
  • 16,511
  • 49
  • 132
  • 234
2
votes
1 answer

Profiling C extension which calls back into Python

Suppose for the purpose of this discussion, I have a function like this: PyObject* tuple_from_dict(PyObject* ftype, PyObject* factory, PyObject* values) { PyObject* ttype = PyTuple_GetItem(factory, 1); PyObject* fmapping =…
wvxvw
  • 8,089
  • 10
  • 32
  • 61
2
votes
2 answers

Python C API free() errors after using Py_SetPath() and Py_GetPath()

I'm trying to figure out why I can't simply get and set the python path through its C API. I am using Python3.6, on Ubuntu 17.10 with gcc version 7.2.0. Compiling with: gcc pytest.c `python3-config --libs` `python3-config --includes` #include…
cowemoji
  • 39
  • 5
2
votes
1 answer

C++ Python embedding: Run on machine with no Python?

I'm trying to make a small game that supports Python scripting. I've no problems with using the Python C-API, but I don't know how to ensure that the game will run on a computer with no Python installed. I know I need pythonXY.dll -- what else is…
Paul
  • 269
  • 4
  • 11
2
votes
3 answers

How do I get the current PyInterpreterState?

I'd like to create a PyThreadState since there doesn't appear to be one for the current thread. How do I get the current PyInterpreterState to pass to PyThreadState_New(...), or is that something that I should only do if I'm embedding an interpreter…
James
  • 24,676
  • 13
  • 84
  • 130
2
votes
1 answer

How to change function parameter values in a Python 3 C extension?

I can't figure out how to change the value of a parameter passed from Python to C. PyArg_ParseTuple (args, "Os", &file_handle, &filename) will let me get file_handle as a PyObject *. Is there a way to change the value file_handle represents? I…
2
votes
1 answer

(Python/C API) "ModuleNotFoundError" for modules in subdirectories

I am using Python 3 within a C program. What I wish to do is "run" a single Python file (.py) which will be the main file for a larger Python project. When I use import within this Python file, it works fine for other Python files in the same…
Griffort
  • 1,174
  • 1
  • 10
  • 26
2
votes
0 answers

Create an Array from the Python C API without numpy

Is it possible to create a Python Array in C using the C-API? If so how do I do this? Is arraymodule.c the right place to be looking in the CPython source? If I was using numpy I'd do this…
shuttle87
  • 15,466
  • 11
  • 77
  • 106
2
votes
0 answers

Python shared library - "libpython" naming convention

We are adding support to call python functions from our application. The user inputs the python module path and the python version they want to execute the module in. Valid version strings are any valid python versions of the form 2.7.* and…
Chethan Ravindranath
  • 2,001
  • 2
  • 16
  • 28
2
votes
1 answer

Error when Importing tensorflow in embedded python in c++

My question is regarding embedding Python 3.5 interpreter in a C++ program to receive an image from C++, and use it as an input for my trained tensorflow model. When I import tensorflow library in my python code I get an error (other libraries work…
Matin H
  • 870
  • 8
  • 16
2
votes
1 answer

Passing two parameters (int and array) to embedded Python function

I need to call Python function from my module and set two parameters for it: int and array. For a now I get segfault during calling this function and I have no idea what I'm doing wrong. Could someone specify where my mistake is? Function in my…
Donz
  • 1,389
  • 1
  • 13
  • 21