How to include __build_class__ when creating a module in the python C API
Question: I am trying to use the [Python 3.5 C API](https://docs.python.org/3/c-api/) to
execute some code that includes constructing a class. Specifically this:

    
    
    class MyClass:
        def test(self):
            print('test')
    
    MyClass().test()
    

The problem I have is that it errors like this:

    
    
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    NameError: __build_class__ not found
    

So somehow I need my module to include `__build_class__`, but I am not sure
how (I guess that I would also miss other things you get by default when using
[Python](https://www.python.org/) too) - is there a way to include all this
built-in stuff in my module?

Here is my code so far:

    
    
    #include <Python.h>
    
    int main(void)
    {
        int ret = 0;
        PyObject *pValue, *pModule, *pGlobal, *pLocal;
    
        Py_Initialize();
    
        pGlobal = PyDict_New();
        pModule = PyModule_New("mymod");
        pLocal = PyModule_GetDict(pModule);
    
        pValue = PyRun_String(
            "class MyClass:\n\tdef test(self):\n\t\tprint('test')\n\nMyClass().test()",
            Py_file_input,
            pGlobal,
            pLocal);
    
        if (pValue == NULL) {
            if (PyErr_Occurred()) {
                PyErr_Print();
            }
            ret = 1;
        } else {
            Py_DECREF(pValue);
        }
    
        Py_Finalize();
    
        return ret;
    }
    

so `pValue` is `NULL` and it is calling `PyErr_Print`.


Answer: Their are (at least) two ways to solve this it seems...

## Way 1

Instead of:

    
    
    pGlobal = PyDict_New();
    

You can import the `__main__` module and get it's globals dictionary like
this:

    
    
    pGlobal = PyModule_GetDict(PyImport_AddModule("__main__"));
    

This way is described like so:

> BUT PyEval_GetGlobals will return null it it is not called from within
> Python. This will never be the case when extending Python, but when Python
> is embedded, it may happen. This is because PyRun_* define the global scope,
> so if you're not somehow inside a PyRun_ thing (e.g. module called from
> python called from embedder), there are no globals.
>
> In an embedded-python situation, if you decide that all of your PyRun_*
> calls are going to use `__main__` as the global namespace,
> PyModule_GetDict(PyImport_AddModule("`__main__`")) will do it.

Which I got from the question [embedding](http://www.gossamer-
threads.com/lists/python/python/8946#8946) I found over on this [Python
list](http://www.gossamer-threads.com/lists/python/python/).

## Way 2

Or as an alternative, which I personally prefer to importing the main module
(and found [here](http://stackoverflow.com/a/10684099/1039947)), you can do
this to populate the new dictionary you created with the built-in stuff which
includes `__build_class__`:

    
    
    pGlobal = PyDict_New();
    PyDict_SetItemString(pGlobal, "__builtins__", PyEval_GetBuiltins());
    

