Python Embedded application failing to import http.server

importFail.py

from http.server import BaseHTTPRequestHandler, HTTPServer
import sys; print(sys.path)
def runSimp():
	print("foo")
runSimp()

py-embed-thread.cpp

#include <Python.h>

void runPy() {
    Py_SetPath(L"/Users/xxx/p/python-embed/libs/cpython/Lib:/Users/xxx/p/python-embed/src/app");
    Py_SetPythonHome(L"/Users/xxx/p/python-embed/libs/cpython/Lib:/Users/xxx/p/python-embed/libs/cpython/build/lib.macosx-11.5-x86_64-3.10");

    Py_Initialize();
    if (!Py_IsInitialized()) {
        printf("Failed to initialize python interpreter.");
    }
}

int main(int argc, char *argv[]) {
    // import fail
    runPy();
    PyObject *mymod = PyImport_ImportModule("importFail");
	PyObject *strfunc = PyObject_GetAttrString(mymod, "runSimp");
	PyObject *strargs = Py_BuildValue("()");
	PyObject *ret = PyEval_CallObject(strfunc, strargs);
     return 0;
}

The python exe imports it fine, but then when I use my embedded compilation it fails. Possible problems include loading shared objects. I can run lldb on the application and set breakpoints.

You don’t have error handling in the C code you posted. In particular, in main() check that the return values of the python APIs are not null, all of them can fail and will return NULL when, and raise a Python exception;

Something like:

PyObject* mymod = ...;
if (mymod == NULL) {
    PyErrr_Print();
    return;
}

Adding error checking won’t fix the error, but could give you hints as to what goes wrong.