python-embedding

How is it possible to access a python module in C++?

两盒软妹~` 提交于 2020-04-18 03:45:29
问题 I'm trying to import a module in C++. the module resides in a package and should be accessed like: from x.y import class1,func1, const1, etc I'm on Python3.6 and for this version what I have found so far is to use PyRun_SimpleString to do the import and then use PyImport_AddModuleObject to have a handle to my module. that is: PyRun_SimpleString("from PacKage1 import Module1 as Module1"); auto module = PyImport_AddModuleObject(PyUnicode_DecodeFSDefault("Module1")); so that down the road I can

Passing array/tuple from python back to c++

狂风中的少年 提交于 2020-04-17 20:38:27
问题 I am trying to pass a list to python from cpp and taking it back. Initially I tried to pass a single value and get back one value. It worked. Now I am trying to pass the complete array/list Below is my cpp code: #include <iostream> #include <Python.h> #include <numpy/arrayobject.h> #include <typeinfo> using namespace std; int main() { Py_Initialize(); PyObject *sys = PyImport_ImportModule("sys"); PyObject *path = PyObject_GetAttrString(sys, "path"); PyList_Append(path, PyString_FromString("."

Convert opencv Mat to PyObject*

岁酱吖の 提交于 2020-03-03 09:09:11
问题 I've tried to convert OpenCV Mat to PyObject* with this code : PyObject* ConvertMatToPyArray(const cv::Mat& mat) { npy_intp dims[3] = { mat.rows, mat.cols, mat.channels() }; const int ND = 3; PyObject *pArray = PyArray_SimpleNewFromData( ND, dims, NPY_UINT8, reinterpret_cast<void*>(mat.data)); return pArray; } but always raise this exception : Exception thrown at 0x00007FF75B5EDF03 in EmbededPython.exe: 0xC0000005: Access violation reading location 0x0000000000000010. Where I mistake? or How

Embedded Python: Multiple Sub-Interpreters not working

99封情书 提交于 2020-01-16 19:35:08
问题 I'm trying to understand sub-interpreters and GIL. But my experiment is failing often(The same code rarely works). Gathering info from SO questions and few sites, I have the following code which spawns 2 non-python threads. Each of these threads are given a python sub-interpreter. I want to release GIL inside these threads and call a DLL function in C++(though this example does not detail that. Here I just write to stdout). Basically I want to see concurrency in the execution(Non-Python DLL

Embedding Python into C - importing modules

只谈情不闲聊 提交于 2020-01-12 05:25:09
问题 I am having problems using the Embedded Python for C as per the Documentation - Whenever I try using imported modules I get an : Unhandled exception at 0x1e089e85 in PythonIncl.exe: 0xC0000005: Access violation reading location 0x00000004. The error occurs in the PyObject_GetAttrString() method and the documentation isn't much help. I have also tried using tutorials as in an Example from IBM, but always get the same access violation. The following is the example code from one of the tutorials

Using Boost::Python::Object causes linker errors

隐身守侯 提交于 2020-01-03 01:21:47
问题 So, I'm attempting to embed Python into C++. I have gotten fairly far in, and have been able to do basic things like run strings of Python. As soon as I tried to use Boost::Python::Object I began getting these 4 linker errors. I built boost using BJAM with Boost 1.54.0 and Python 2.7.5. Python Lib Build Commands: bootstrap .\b2 toolset=msvc-10.0 --with-python Minimal Code Example : #include <boost/python.hpp> #include <iostream> int main(int, char **) { Py_Initialize(); PyRun_SimpleString(

python embedding: passing list from C to python function

不想你离开。 提交于 2020-01-01 17:05:53
问题 Trying to pass a list to python from C++ is not working. Here is the relevant code ( written using other related postings): Py_Initialize(); PyObject *sys = PyImport_ImportModule("sys"); PyObject *path = PyObject_GetAttrString(sys, "path"); PyList_Append(path, PyString_FromString(".")); // Build the name object pName = PyString_FromString("MySolver"); // Load the module object pModule = PyImport_Import(pName); // pDict is a borrowed reference pDict = PyModule_GetDict(pModule); // pFunc is

embedding python error on initialization

不问归期 提交于 2019-12-23 22:07:41
问题 when im running C code to call python functions, there's error on Py_Initialize() The error is ImportError: No module named site. Ive tried to put Py_SetProgramName(argv[0]) but it doesnt work. The cmd call is cInterfacePython Test.py multiply 3 2 (exe is cInterfacePython) 回答1: I had to muck about a bit with the PATH env-var as well as PYTHONPATH to make things work better when embedding. Py_SetProgramName is not important, it's mostly for internal reference etc... So, I suggest you find

Embedding python in C, undefined symbol: PyExc_ImportError

杀马特。学长 韩版系。学妹 提交于 2019-12-23 14:21:33
问题 I am trying to write a plug-in for Audacious Media Player that loads a python module. The python embedding code is from the python-2.6 source(embed/Demo). This compiles with the command line, gcc -o demo demo.c -lpython2.6 -lm -L/usr/lib/python2.6/config I added -lpython2.6 -lm -L/usr/lib/python2.6/config to the CC args. And it loads a Python script which imports pygtk and gtk modules, this works fine. But after I compile the plug-in(a shared library) the following error occurs(this is not

Printing a variable in an embedded Python interpreter

China☆狼群 提交于 2019-12-23 12:59:26
问题 I have written a small C program that embeds Python. I'm setting it up correctly using Py_Initialize() and Py_Finalize(), and am able to run scripts either using PyRun_SimpleString or PyRun_SimpleFile. However, I don't know how mimic the behavior of Python's own interpreter when printing variables. Specifically: a = (1, 2, 3) print a Works fine for me: it prints out (1, 2, 3) However: a = (1, 2, 3) a Prints out nothing at all. In Python's own interpreter, this would print out (1, 2, 3) as