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 6179049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:34:48+00:00 2026-05-24T00:34:48+00:00

I have a Python module that I import from my C++ code (I’m embedding

  • 0

I have a Python module that I import from my C++ code (I’m embedding Python). This module contains a function create() that I want to get a hold of in my C++ code (i.e. store it in a boost::python::object instance).

Here’s what I tried. A run-time error occurs on the indicated line in my C++ code. The error occurs because it is unable to find the “hero.create” function inside the main namespace.

C++ code

namespace python = boost::python;

// Standard Boost.Python code
// Here I just create objects for the main module and its namespace

python::object main_module(
    python::handle<>(python::borrowed(PyImport_AddModule("__main__")))
);

python::object main_namespace(main_module.attr("__dict__"));


// This is my code
//

python::exec("import hero", main_namespace, main_namespace);
python::object func(main_namespace["hero.create"]); // Run-time error
Entity ent = python::extract<Entity>(func());

// I also tried doing this, but it didn't work either...
// python::object func(main_namespace["hero"].attr("__dict__")["create"]);

// However, if I do this, all works fine...
// python::exec("from hero import create", main_namespace, main_namespace);
// python::object func(main_namespace["create"]); // No error

Python code (hero.py)

from entity import Entity

def create():
    ent = Entity()
    # ...
    return ent
  • 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. Editorial Team
    Editorial Team
    2026-05-24T00:34:48+00:00Added an answer on May 24, 2026 at 12:34 am

    You need to do main_namespace["hero"].attr("create"). Import creates only one name in the namespace, and it’s a module object. Names cannot have dots in them — . is a getattr operator — so hero.create is the same as getattr(hero, 'create').

    You could also use boost::python::import directly, instead of execing import statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code in a C application that's embedding Python (2.7.1): { PyObject
I have a python module that makes use of a huge dictionary global variable,
I have a python module that defines a number of classes: class A(object): def
I have created a Python module that creates and populates several SQLite tables. Now,
I have a Python program that uses the threading module. Once every second, my
I have Python 2.6 and I want to install easy _ install module. The
I have a Python module, wrapper.py , that wraps a C DLL. The DLL
I have to port an algorithm from an Excel sheet to python code but
I'm having a module import issue. using python 2.6 on ubuntu 10.10 I have
I have a python function that may raise an exception. The caller catches the

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.