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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:09:17+00:00 2026-06-03T01:09:17+00:00

Im currently trying to embed the python interpreter into my application. Because my application

  • 0

Im currently trying to embed the python interpreter into my application. Because my application uses the Poco API for logging, I want to make it accessable through the logging module in python too.
The most easiest way for me to do this, is to provide a static set of function as an extension module to log a message and then to write a Handler subclass calling these functions.

Since I dont want the user to install any additional python modules and since I dont have the requirement to reuse my code outside of my embedded python interpreter, it would be great if one could just provide the static functions through Py_InitModule() and then to add a hardcoded Handler subclass to the created module (hardcoded means: added at runtime but actually a const string which gets always interpreted at initialization).

My problem is that I dont know how to interpret a normal python class definition, e.g:

class Test:
    someVar=1

so that it is added to a given module and then accesable as, e.g mymodule.Test

A solution can either be pure python based or work with the python c-api.

  • 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-03T01:09:19+00:00Added an answer on June 3, 2026 at 1:09 am

    I finally found the answere myself:
    There are actually 2 methods to execute code in the context of a module

    Way 1

    PyObject* module = Py_InitModule("poco",LoggingPocoMethods);
    PyObject* code = Py_CompileString("class Test:\n\tdef __repr__(self):\n\t\treturn 'Hello world'","",Py_file_input);
    PyImport_ExecCodeModule("poco",code);
    

    This method has the drawback that the module will be reloaded which is not required at this stage.

    Way 2

    PyObject* module = Py_InitModule("poco",LoggingPocoMethods);
    PyObject* mainModule = PyImport_AddModule("__main__");
    
    PyObject* dict = PyModule_GetDict(module);
    PyObject* mainDict = PyModule_GetDict(mainModule);
    
    PyRun_String("def test():\n\tprint 'test'\n",Py_file_input,mainDict,dict);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to embed a swank-clojure repl into my application for the purpose of
I am trying to currently embed a media player into certain web content and
We are currently trying to embed a swf into our page using https. That
I am trying to embed a flash movie (.flv) into a webpage with a
Currently I am trying to embed a Java applet onto a webpage, and the
In one particular project, we're trying to embed version information into shared object files.
I am currently trying to embed this project xna console in my game but
I'm trying to embed a window from my process into the window of an
I'm trying to embed python code in C++ (Windows 7 + minGW + Python
I'm trying to get the following functionality: Embed a youtube video into a webpage

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.