Questions tagged [python-embedding]

Questions regarding embedding Python interpreter in other applications, such that it may serve as a scripting language.

When embedding Python, the interface code does:

  • Convert data values from C to Python,

  • Perform a function call to a Python interface routine using the converted values, and

  • Convert the data values from the call from Python to C.

Reference

309 questions
90
votes
10 answers

Calling a python method from C/C++, and extracting its return value

I'd like to call a custom function that is defined in a Python module from C. I have some preliminary code to do that, but it just prints the output to stdout. mytest.py import math def myabs(x): return math.fabs(x) test.cpp #include…
D R
  • 21,936
  • 38
  • 112
  • 149
52
votes
1 answer

Create an object using Python's C API

Say I have my object layout defined as: typedef struct { PyObject_HEAD // Other stuff... } pyfoo; ...and my type definition: static PyTypeObject pyfoo_T = { PyObject_HEAD_INIT(NULL) // ... pyfoo_new, }; How do I create a new…
detly
  • 29,332
  • 18
  • 93
  • 152
30
votes
3 answers

How To catch python stdout in c++ code

I have a program which during it's run sometimes needs to call python in order to preform some tasks. I need a function that calls python and catches pythons stdout and puts it in some file. This is a declaration of the function …
alexpov
  • 1,158
  • 2
  • 16
  • 29
29
votes
1 answer

Multiple independent embedded Python Interpreters on multiple operating system threads invoked from C/C++ program

Embedding Python interpreter in a C/C++ application is well documented. What is the best approach to run multiple python interpreter on multiple operating system threads (i.e. one interpreter on one operating system thread within the same process)…
bhadra
  • 12,887
  • 10
  • 54
  • 47
19
votes
4 answers

Embedding python in multithreaded C application

I'm embedding the python interpreter in a multithreaded C application and I'm a little confused as to what APIs I should use to ensure thread safety. From what I gathered, when embedding python it is up to the embedder to take care of the GIL lock…
shoosh
  • 76,898
  • 55
  • 205
  • 325
17
votes
2 answers

Is it possible to embed PyPy into a .NET application?

I would like to embed a Python interpreter into my .NET application. I'm aware of IronPython, of course, but I'm specifically interested in PyPy, because of its stackless support and microthreads. However, while PyPy can be built against the CLI, it…
Joe White
  • 94,807
  • 60
  • 220
  • 330
17
votes
2 answers

Numpy import fails on multiarray extension library when called from embedded Python within a C++ application

I'm running a C++ application which tries to run python using the https://docs.python.org/3.5/extending/embedding.html function calls. This is the error that the application error message pipes are giving me. class 'ImportError': Importing the…
skincell
  • 407
  • 3
  • 7
16
votes
2 answers

Where does my embedded python stdout go?

Consider the following MWE: #include #include int main(void) { printf("Test 1\n"); Py_Initialize(); printf("Test 2\n"); PyRun_SimpleString("print('Test 3')"); printf("Test 4\n"); return 0; } When I compile and run…
A. Nilsson
  • 539
  • 3
  • 5
  • 19
14
votes
1 answer

Fatal Python error when using a dynamic version of Python to execute embedded python code

SPOILER: partially solved (see at the end). Here is an example of code using Python embedded: #include int main(int argc, char** argv) { Py_SetPythonHome(argv[1]); Py_Initialize(); PyRun_SimpleString("print \"Hello !\""); …
Caduchon
  • 4,574
  • 4
  • 26
  • 67
13
votes
3 answers

Limiting execution time of embedded Python

If I embed the Python interpreter in a C or C++ program, as in this example, is there any way to limit how long the interpreter runs for? Is there anything to stop the Python code from entering an infinite loop and thus preventing…
user200783
  • 13,722
  • 12
  • 69
  • 135
12
votes
4 answers

How to make cmake find pybind11

I am trying to follow the simple example for embedding python within c++ using pybind11 as found on this page. However, when trying to use cmake to build the solution, I keep getting an error that says By not providing "Findpybind11.cmake" in…
bwolf
  • 167
  • 1
  • 1
  • 6
12
votes
1 answer

"AttributeError: 'module' object has no attribute 'argv'" when using Python.h

When messing around with Python.h I got this error: AttributeError: 'module' object has no attribute 'argv' C++ code: #include "stdafx.h" #include "C:/Python27/include/Python.h" #include using namespace std; int main() …
Chengy
  • 619
  • 1
  • 7
  • 17
11
votes
7 answers

Is it possible to modify PYTHONPATH at runtime?

I have a C++ application dynamically linked to the Python interpreter. I want to be able to import python modules from a particular directory. I want to modify the PYTHONPATH for my process so that sys.path will include the paths that I added to…
Skrymsli
  • 5,173
  • 7
  • 34
  • 36
11
votes
1 answer

Embedding a matplotlib chart into Qt/C++ application

I am developing an math-oriented GUI application in Qt/C++ and would like to embed a Python scripting, including NumPy and Matplotlib. Using Python C API, I finally managed to run a script, retrieve the values from the Python variables, including…
10
votes
4 answers

Linear algebra on iPhone (python/numpy?)

This semester, I'm implementing a compressed-sensing algorithm as an iPhone app. To do this, I'll need some good matrix/linear algebra libraries. I'm a little new to both iOS and Python, and am looking for some help at evaluating my options. I know…
Scott
  • 2,568
  • 1
  • 27
  • 39
1
2 3
20 21