python-embedding

Which standard library modules are required to run the Python 3.5 interpreter?

こ雲淡風輕ζ 提交于 2019-11-30 03:28:09
问题 Here's a CPython program that tries to initialize the interpreter with an empty sys.path : #include <Python.h> int main(int argc, char** argv) { wchar_t* program = NULL; wchar_t* sys_path = NULL; Py_NoSiteFlag = 1; program = Py_DecodeLocale(argv[0], NULL); Py_SetProgramName(program); sys_path = Py_DecodeLocale("", NULL); Py_SetPath(sys_path); Py_Initialize(); PyMem_RawFree(program); PyMem_RawFree(sys_path); Py_Finalize(); } Executing the program above raises the following error: Fatal Python

Embedding python in multithreaded C application

依然范特西╮ 提交于 2019-11-29 01:24:25
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 before calling any other Python C API call. This is done with these functions: gstate = PyGILState_Ensure(); // do some python api calls, run python scripts PyGILState_Release(gstate); But this alone doesn't seem to be enough. I still got random crashes since it doesn't seem to provide mutual exclusion for the Python APIs. After reading some more

Why does PyImport_Import fail to load a module from the current directory?

人走茶凉 提交于 2019-11-28 10:06:29
I'm trying to run the embedding example and I can't load a module from the current working directory unless I explicitly add it to sys.path then it works: PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append(\".\")"); Shouldn't Python look for modules in the current directory ? Edit1 : Tried just importing the module with: Py_Initialize(); PyRun_SimpleString("import multiply"); And it still fails with the following error: Traceback (most recent call last): File "<string>", line 1, in <module> ImportError: No module named multiply Edit2 : From the sys.path docs : If the script

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

陌路散爱 提交于 2019-11-28 06:52:00
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) which are invoked from the C/C++ application? Such applications may also have problems related to memory fragmentation and limitations of Py_Finalize() . One such approach can be the following: Python thread and hence GIL disabled in pyconfig.h to keep it simple (#undef WITH_THREAD) All mutable global variables of Python Interpreter source code

Embedding python in multithreaded C application

我怕爱的太早我们不能终老 提交于 2019-11-27 15:53:40
问题 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 before calling any other Python C API call. This is done with these functions: gstate = PyGILState_Ensure(); // do some python api calls, run python scripts PyGILState_Release(gstate); But this alone doesn't seem to be enough. I still got random

Create an object using Python's C API

一曲冷凌霜 提交于 2019-11-27 07:04:35
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 instance of pyfoo somewhere within my C extension? Call PyObject_New() , followed by PyObject_Init() . EDIT: The best way is to call the class object, just like in Python itself: /* Pass two arguments, a string and an int. */ PyObject *argList = Py_BuildValue("si", "hello", 42); /* Call the class object. */ PyObject *obj = PyObject_CallObject((PyObject *) &pyfoo_T, argList

Why does PyImport_Import fail to load a module from the current directory?

雨燕双飞 提交于 2019-11-27 03:24:55
问题 I'm trying to run the embedding example and I can't load a module from the current working directory unless I explicitly add it to sys.path then it works: PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append(\".\")"); Shouldn't Python look for modules in the current directory ? Edit1 : Tried just importing the module with: Py_Initialize(); PyRun_SimpleString("import multiply"); And it still fails with the following error: Traceback (most recent call last): File "<string>",

How To catch python stdout in c++ code

房东的猫 提交于 2019-11-27 03:12:26
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 python API and I don't know what is the right technique to do this. First thing I've tried is to redirect python's sdtout and stderr using Py_run_SimpleString this is some example of the code i've written. #include "boost

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

人走茶凉 提交于 2019-11-27 01:33:16
问题 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) which are invoked from the C/C++ application? Such applications may also have problems related to memory fragmentation and limitations of Py_Finalize(). One such approach can be the following: Python thread and hence GIL disabled in pyconfig.h to

Python C API doesn't load module

半腔热情 提交于 2019-11-26 23:26:44
问题 I'm trying to load a python module that contains a math and numpy import in C, using the C API. I can load and run the module but, if I import the math module it doesn't work. I'm using Arch Linux, Python 2.7.2 and gcc. Here the codes: #include <stdio.h> #include <stdlib.h> #include <python2.7/Python.h> int main(int argc, char **argv) { PyObject *pName, *pModule, *pFunc, *pArg, *pDict, *pReturn, *pT1, *pT2, *pX, *pY; int i; double x, y; Py_Initialize(); PySys_SetPath("."); pName = PyString