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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:25:43+00:00 2026-05-26T14:25:43+00:00

Here is canonical example of a program extending embedded Python 3.x in C/C++: #include

  • 0

Here is canonical example of a program extending embedded Python 3.x in C/C++:

#include <Python.h>
//// Definition of 'emb' Python module ////////////////////
static PyObject* emb_foo(PyObject *self, PyObject *args)
{
    char const* n = "I am foo";
    return Py_BuildValue("s", n);
}
static PyMethodDef EmbMethods[] = {
    {"foo", emb_foo, METH_VARARGS, "Returns foo"},
    {NULL, NULL, 0, NULL}
};
static PyModuleDef EmbModule = {
    PyModuleDef_HEAD_INIT, "emb", NULL, -1, EmbMethods,
    NULL, NULL, NULL, NULL
};
static PyObject* PyInit_emb(void)
{
    return PyModule_Create(&EmbModule);
}
//// Embedded Python with 'emb' loaded ////////////////////
int main()
{
    PyImport_AppendInittab("emb", &PyInit_emb);
    Py_Initialize();

    PyRun_SimpleString("import emb\n");       // (1)
    //PyImport_ImportModule("emb");           // (2)

    PyRun_SimpleString("print(emb.foo())\n"); // (3)

    Py_Finalize();
    return 0;
}

I add the emb module to built-ins of the embedded interpreter.
I’d also like to import it automatically, so users don’t have to issue import emb statement in their scripts supplied to my embedded interpreter.
I’m trying two ways of importing, in lines (1) and (2).

The (1) works and the emb module can be found without explicit import in the simple test in line (3). However, if I comment out the line (1) and uncomment the line (2) to import with C API of Python 3 call, then the line (3) produces error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'emb' is not defined

I’d like to understand what is the difference here between the two manners of importing.
Do they import module into different namespaces / scopes ?

The Python 3 documentation led me along this path:

  1. PyImport_ImportModule is best described by referring to the built-in Python function __import__()
  2. __import__() function is invoked by the import statement.

Perhaps I made a mistake assuming PyImport_ImportModule is one-to-one equivalent and I should be using PyImport_ImportModuleEx with correct (which exactly?) globals and locals, so my ’emb’ lands in global namespace of my embedded interpreter.

  • 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-26T14:25:44+00:00Added an answer on May 26, 2026 at 2:25 pm

    __import__ doesn’t put the module in any namespace at all, but returns it instead.
    import calls __import__, plus it stores the result in a variable.
    The docs say that import spam does something similar to:

    spam = __import__('spam', globals(), locals(), [], 0)
    

    To get the same effect in the C API, you need to assign to the emb global. In other words, set the emb attribute on the __main__ module.

    PyObject* emb_module = PyImport_ImportModule("emb");
    PyObject* main_module = PyImport_AddModule("__main__");
    PyObject_SetAttrString(main_module, "emb", emb_module);
    Py_XDECREF(emb_module);
    /* (main_module is a borrowed reference) */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The canonical example here is Twitter's API. I understand conceptually how the REST API
part-time C# programmer here trying to learn Python. I am looking to be able
A canonical example of patching up an otherwise covariant class is as follows: abstract
I am trying to write a C++ program for the canonical genetic algorithm ,
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is a simplification of my database: Table: Property Fields: ID, Address Table: Quote
Here is my code, which takes two version identifiers in the form 1, 5,

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.