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
0 answers

Python throws a ModuleNotFoundError when loaded from C, but not when directly executed

I'm in the process of making a module called BSPy. It's not really relevant what the module does, but its setup.py is: from distutils.core import setup, Extension setup(name='BSPy', version='0.0.2', ext_modules=[Extension('BSPy',…
Mitchell Faas
  • 430
  • 6
  • 19
1
vote
1 answer

Python binding C++ virtual member function cannot be called

I recently wrote an extension to Python 3 in C++, but I encountered some trouble when I called C++ in python, and I don't plan to use a third-party library. I'm used Python binding C++ virtual member function cannot be called, but removing the…
Jerry
  • 45
  • 5
1
vote
1 answer

Compile and execute AST using pybind11 or Python C API

I would like to translate this Python code into C++ code using Pybind11 or directly the Python C API: import ast code = "print('Hello World!')" code_ast = ast.parse(code, mode="exec") # "code" being a string containing code # ... perform some…
1
vote
1 answer

C Python Module -- ImportError: Symbol not found: _Py_InitModule4_64

I am beginning the process of writing Python 3 module in C. The C I have written already compiles fine (code I compiled at bottom of post). I compile with: python3 setup.py build_ext --inplace The built .so file is placed in the current…
Taras Palczynski
  • 189
  • 1
  • 10
1
vote
0 answers

Variables not getting garbage collected causing C++ process memory to increase

I have a Python script like this: import module globalvariable a def read(key): x = funcA(a, key) ret = funcB(x) if ret == True: return "Success" else: return "Failure" I am calling this function from my C++ program…
Venkat Krishnan
  • 107
  • 1
  • 2
  • 8
1
vote
0 answers

C++ embedded python and environment variables

I have create a C++ application that use python using the python C++ library and I have temporarily change my PATH variable into C++ using this code GetEnvironmentVariable( _T("PATH"), szSysPath.GetBuffer(32000), 32000) ; szSysPath.ReleaseBuffer()…
belmat
  • 11
  • 1
1
vote
1 answer

Python C-API equivalent of "python -m module_name" (python 2.7)

Having looked the Python C-API documentation, I cannot figure out a way to execute a python script given just the name of the module (from c or c++ code). Is there an equivalent C-API function for python -m module_name? I am using Python 2.7…
Eddy
  • 6,661
  • 21
  • 58
  • 71
1
vote
1 answer

Python C-API and Numpy: core dump on import_array

//testNumpy.c #include #include #include int main(){ printf("import_array\n");fflush(stdout); import_array(); printf("import_array done\n");fflush(stdout); } $ gcc -I/usr/include/python2.6…
Jeff Guy
  • 157
  • 1
  • 9
1
vote
0 answers

Python C-API module initialization handler, complement of Py_AtExit

In a python script(module) any global function call will be executed each time I import/reload module. I call these module-level initialization function. ex. def init(): print "Hello, this is module initialization function" print "init()…
Shady Atef
  • 2,121
  • 1
  • 22
  • 40
1
vote
0 answers

CMake linking to the result of a subshell

I am having trouble linking python 3 in cmake. The options to link in the shell are $(/usr/bin/python3-config --ldflags) and that works completely fine. But when I move to cmake, I use: set(Python_ADDITIONAL_VERSIONS 3.5) find_package(PythonLibs…
ar2015
  • 5,558
  • 8
  • 53
  • 110
1
vote
1 answer

Dealing with error LNK2001: unresolved external symbol __imp__ExitWindowsEx@8 in Python extensions

I am trying to build a C project. I think the source is fine, however I am getting this error: error LNK2001: unresolved external symbol __imp__ExitWindowsEx@8 Or for the complete traceback: C:\Users\Simon\Desktop\Learn>python setup.py…
Xantium
  • 11,201
  • 10
  • 62
  • 89
1
vote
0 answers

How to cast printf() from C function into a python data structure?

EDIT: This question is related to this: Python ctypes: how to pass row outputs from a C function into a pandas DataFrame? My question is how to parse text printed to STDOUT via printf() into something useable by Python. I'm working with a Python…
ShanZhengYang
  • 16,511
  • 49
  • 132
  • 234
1
vote
1 answer

Python C-extension submodule Error: no module named "x"

I'm having difficulty with making a C-extension as a submodule in my code. The C extension below compiles just fine. The problem occurs when I attempt to add it to another module. Here's the C code: The file name is prctl3-0.c. I am able to get it…
A. Reed
  • 13
  • 5
1
vote
1 answer

Python C extension segfault

I'm venturing into C extensions for the first time, and am somewhat new to C as well. I've got a working C extension, however, if i repeatedly call the utility in python, I eventually get a segmentation fault: 11. #include static…
Lzkatz
  • 173
  • 8
1
vote
1 answer

PyRun_File with recursive functions in Python C-API

I am working on embedding python interpreter in a larger system and one of the features is to run a python script on the fly. Testing with the following code snippet works great, but with recursive function only the first call is executed then…
Shady Atef
  • 2,121
  • 1
  • 22
  • 40