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

  • Home
  • SEARCH
  • 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 9267571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:34:58+00:00 2026-06-18T14:34:58+00:00

I am writing a python embedding in C++ application. The related code snippet that

  • 0

I am writing a python embedding in C++ application. The related code snippet that I declare the module of python(user-defined one) that needs to be embedded as below:

boost::python::object main_module = boost::python::import("__main__");
boost::python::object main_namespace = main_module.attr("__dict__");
boost::python::exec("import python_module", main_namespace); //This line is the culprit

However, I’m stuck when I receive the following error:

terminate called after throwing an instance of 'boost::python::error_already_set'

My user-defined python module resides in the same directory as my C++ code. When I try like to use numpythat works, the problem is that only with my user-defined one, it doesn’t. What could be done in order to debug it?

EDIT:

After I include the code in try/catch block, I obtained the following compile error:

ImportError: No module named python_module

I also try to add this:

boost::python::exec("import sys; sys.path.append('/path/to/python_module.py');",  main_namespace);

boost::python::exec("import python_module", main_namespace);

but not yet working.

The problem now is how do I make it known to my C++ code?

  • 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-06-18T14:34:59+00:00Added an answer on June 18, 2026 at 2:34 pm

    You can try a couple of things:

    1. In your C++ app: boost::python::exec("import sys; sys.path.append('/path/to'); import python_module; del sys"), main_namespace);, or

    2. In your shell: cd /path/to; call-your-c++-app. Then, in C++, you would need only to boost::python::exec("import python_module");, or

    3. Set your environment variable to export PYTHONPATH=/path/to:${PYTHONPATH} and execute your program. Your C++ in this case would need only to boost::python::exec("import python_module"); as in the above solution.

    The issue: You need to append the path leading to the module to sys.path and not the path to the module file itself.

    Another hint: by default, Python will load modules from the current directory. If you cd there and execute your application from that directory, it should find the module on the current directory.

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

Sidebar

Related Questions

I am currently writing an app in python that needs to generate large amount
What are the current rules for writing python code that will pass cleanly through
I am writing a Python application that queries social media APIs via cURL. Most
I'm writing a Python program that, if the user changes settings while running it,
im writing a python script that is being executed in cygwin. one of the
I'm writing a Python app that needs to call some Git commands. I first
I've seen some people writing Python code by creating one class and then an
I'm writing a Python application that takes a command as an argument, for example:
I have been writing Python code for only a couple of weeks, so I'm
I am writing Python code in Visual Studio 2010 using the excellent Python Tools

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.