#include #include namespace bp = boost::python; void workaround_for_local_modules() { PyRun_SimpleString( "import sys\n" "sys.path.append('.')\n"); } int main(int argc, char** argv) { if (argc < 3) { fprintf(stderr,"Usage: call pythonfile funcname [args]\n"); return 1; } Py_Initialize(); workaround_for_local_modules(); try { bp::object module_name; bp::object module; bp::object func; module_name = bp::object(bp::handle<>(PyString_FromString(argv[1]))); module = bp::object(bp::handle<>(PyImport_Import(module_name.ptr()))); func = module.attr(argv[2]); if(func && PyCallable_Check(func.ptr())) { bp::object tuple; tuple = bp::object(bp::handle<>(PyTuple_New(argc - 3))); for(int i = 0; i < argc - 3; ++i) { PyObject* arg; arg = PyInt_FromLong(atoi(argv[i+3])); PyTuple_SetItem(tuple.ptr(), i, arg); } bp::object valueObj; int value; valueObj = bp::object(bp::handle<>(PyObject_CallObject(func.ptr(), tuple.ptr()))); value = bp::extract(valueObj); std::cout << value << std::endl; } } catch(bp::error_already_set const &) { PyErr_Print(); } Py_Finalize(); return EXIT_SUCCESS; }