Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 70783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:47:06+00:00 2026-05-10T19:47:06+00:00

While working on a C++ project, I was looking for a third party library

  • 0

While working on a C++ project, I was looking for a third party library for something that is not my core business. I found a really good library, doing exactly what’s needed, but it is written in Python. I decided to experiment with embedding Python code in C++, using the Boost.Python library.

The C++ code looks something like this:

#include <string> #include <iostream> #include <boost/python.hpp>  using namespace boost::python;  int main(int, char **)  {     Py_Initialize();      try      {         object module((handle<>(borrowed(PyImport_AddModule('__main__')))));          object name_space = module.attr('__dict__');         object ignored = exec('from myModule import MyFunc\n'                           'MyFunc(\'some_arg\')\n',                           name_space);          std::string res = extract<std::string>(name_space['result']);     }      catch (error_already_set)      {         PyErr_Print();     }      Py_Finalize();     return 0; } 

A (very) simplified version of the Python code looks like this:

import thirdparty  def MyFunc(some_arg):     result = thirdparty.go()     print result 

Now the problem is this: ‘MyFunc’ executes fine, i can see the print of ‘result’. What i cannot do is read ‘result’ back from the C++ code. The extract command never finds ‘result’ in any namespace. I tried defining ‘result’ as a global, i even tried returning a tuple, but i cannot get it to work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T19:47:07+00:00Added an answer on May 10, 2026 at 7:47 pm

    First of all, change your function to return the value. printing it will complicate things since you want to get the value back. Suppose your MyModule.py looks like this:

    import thirdparty  def MyFunc(some_arg):     result = thirdparty.go()     return result 

    Now, to do what you want, you have to go beyond basic embedding, as the documentation says. Here is the full code to run your function:

    #include <Python.h>  int main(int argc, char *argv[]) {     PyObject *pName, *pModule, *pFunc;     PyObject *pArgs, *pArg, *pResult;     int i;      Py_Initialize();     pName = PyString_FromString('MyModule.py');     /* Error checking of pName left out as exercise */      pModule = PyImport_Import(pName);     Py_DECREF(pName);      if (pModule != NULL) {         pFunc = PyObject_GetAttrString(pModule, 'MyFunc');         /* pFunc is a new reference */          if (pFunc) {             pArgs = PyTuple_New(0);             pArg = PyString_FromString('some parameter')             /* pArg reference stolen here: */             PyTuple_SetItem(pArgs, 0, pArg);             pResult = PyObject_CallObject(pFunc, pArgs);             Py_DECREF(pArgs);             if (pResult != NULL) {                 printf('Result of call: %s\n', PyString_AsString(pResult));                 Py_DECREF(pResult);             }             else {                 Py_DECREF(pFunc);                 Py_DECREF(pModule);                 PyErr_Print();                 fprintf(stderr,'Call failed\n');                 return 1;             }         }         else {             if (PyErr_Occurred())                 PyErr_Print();             fprintf(stderr, 'Cannot find function');         }         Py_XDECREF(pFunc);         Py_DECREF(pModule);     }     else {         PyErr_Print();         fprintf(stderr, 'Failed to load module');         return 1;     }     Py_Finalize();     return 0; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You're not synchronizing the summing properly to the blockDim.x location.… May 11, 2026 at 11:53 pm
  • Editorial Team
    Editorial Team added an answer As Richard Corden points out, this issue was addressed in… May 11, 2026 at 11:53 pm
  • Editorial Team
    Editorial Team added an answer Not that I know of. In particular, you can define… May 11, 2026 at 11:53 pm

Related Questions

I'm writing an editor for large archive files (see below) of 4GB+, in native&managed
I'm in the process of porting a large C++ application from Linux (gcc) to
I'm currently working on a ray-tracer in C# as a hobby project. I'm trying
I am working on a group senior project for my university and I have

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.