python-embedding

Embedding python + numpy code into C++ dll callback

孤人 提交于 2019-12-23 09:40:26
问题 I am new of python embedding. I am trying to embed python + numpy code inside a C++ callback function (inside a dll) the problem i am facing is the following. if i have: Py_Initialize(); // some python glue // python invocation Py_Finalize(); everything works fine. but if i have: Py_Initialize(); _import_array(); //to initialize numpy C-API // some python glue + numpy array object creation // python invocation via PyObject_CallObject() Py_Finalize(); this crashes at the second time it reaches

pythonnet Embedding Python in .net example failing to load module

跟風遠走 提交于 2019-12-22 11:31:17
问题 I'm trying to run the Embedding Python in .NET example from https://github.com/pythonnet/pythonnet. I've followed troubleshooting articles to set the proper %PYTHONPATH% and %PYTHONHOME% to my anaconda environment in the program base directory. After activating my anaconda environment, I have successfully imported sys, and imp as a test, and also sucessfully used PythonEngine.RunSimpleString(), but the numpy example fails with Python.Runtime.PythonException: ImportError : No module named

Embedding Python in C: Error in linking - undefined reference to PyString_AsString

风流意气都作罢 提交于 2019-12-22 06:46:29
问题 I am trying to embed a python program inside a C program. My OS is Ubuntu 14.04 I try to embed python 2.7 and python 3.4 interpreter in the same C code base (as separate applications). The compilation and linking works when embedding python 2.7 but not for the python 3.4. It fails during the linker stage. Here is my C code (just an example not real code) simple.c #include <stdio.h> #include <Python.h> int main(int argc, char *argv[]) { PyObject *pName, *pModule, *pFunc, *pValue; char module[]

Error when Importing tensorflow in embedded python in c++

China☆狼群 提交于 2019-12-19 10:42:39
问题 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 fine). The simplified code is as follows: #include <string> #include <windows.h> #include <stdio.h> int main() { Py_InitializeEx(1); PyObject* sysPath = PySys_GetObject((char*)"path"); PyObject* curDir = PyUnicode_FromString("."); PyList_Append

Embedded Python 2.7.2 Importing a module from a user-defined directory

一个人想着一个人 提交于 2019-12-19 01:19:06
问题 I'm embedding Python into a C/C++ application that will have a defined API. The application needs to instantiate classes defined in a script, which are structured roughly like this: class userscript1: def __init__(self): ##do something here... def method1(self): ## method that can be called by the C/C++ app...etc I've managed in the past (for the proof-of-concept) to get this done using the following type of code: PyObject* pName = PyString_FromString("userscript.py"); PyObject* pModule =

(Python C API) PyRun_StringFlags missing builtin functions?

我们两清 提交于 2019-12-18 20:08:45
问题 I am trying to embed some python in my pet project. I have reduced my problem to the following code: #include <Python.h> #include "iostream" int main(int argc, char *argv[]) { Py_Initialize(); PyObject *globals = Py_BuildValue("{}"); PyObject *locals = Py_BuildValue("{}"); PyObject *string_result = PyRun_StringFlags( "a=5\n" "s='hello'\n" "d=dict()\n" , Py_file_input, globals, locals, NULL); if ( PyErr_Occurred() ) {PyErr_Print();PyErr_Clear();return 1;} return 0; } (I know I'm not cleaning

How To catch python stdout in c++ code

ⅰ亾dé卋堺 提交于 2019-12-17 07:12:07
问题 This question was migrated from Server Fault because it can be answered on Stack Overflow. Migrated 9 years ago . 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 pythonCallBackFunc(const char* pythonInput) My problem is to catch all the python output for a given command (pythonInput). I have no experience with

boost python, using a namespace other than main global

两盒软妹~` 提交于 2019-12-13 07:06:49
问题 I am embedding python in my C++ application using boost python. I am a C++ programmer, with very limited knowledge of Python. I have a C++ class, PyExpression . Each instance of this class has a string expStr , which is a short user-entered (at runtime) python program, that is executed by calling boost::python::exec . Briefly, I have this set up as: //import main and its globals bp::object main = bp::import("__main__"); bp::object main_namespace = main.attr("__dict__"); where main and main

can not import numpy in boost-python

风流意气都作罢 提交于 2019-12-13 00:52:48
问题 I am trying out boost-python. However, even a simple hello world doesn't work. #define BOOST_PYTHON_STATIC_LIB #include <boost/python/detail/wrap_python.hpp> #include "numpy/arrayobject.h" #include <boost/python.hpp> void init_numpy() { import_array(); } int main() { Py_Intialize(); init_numpy(); } It gives error ImportError: numpy.core.multiarray failed to import But if I open my IPython, and run import numpy.core.multiarray , it runs fine. What part am I getting worng? 回答1: I solved the

Passing a List and numpy.matrix to a python function from a C++ application

怎甘沉沦 提交于 2019-12-12 01:19:42
问题 I have a bunch of functions written in python (for rapid prototyping). My main project is in C++ and I wanna call these functions from my C++ program.These functions use some specialized python modules like numpy, pyside etc. To start with, I have one function that takes in 4 arguments. The first one is a numpy.matrix object and the other three are simple python lists. The function is returning a numpy.matrix object. I know I'm supposed to use a combination of Python/C API and Numpy/C API,