#include #include "Python.h" int main() { // in main thread Py_Initialize(); PyEval_InitThreads(); PyEval_SaveThread(); // creating a new sub-interpreter, in my real app, this would be created // in a sub-thread. PyInterpreterState* const mainInterpreterState = PyInterpreterState_New(); PyThreadState* const taskState = PyThreadState_New(mainInterpreterState); PyEval_AcquireThread(taskState); PyThreadState * const sub_interp = Py_NewInterpreter(); PyThreadState_Swap( taskState ); PyEval_ReleaseThread( taskState ); // may execute in another separate sub-thread PyThreadState * const tstate = PyThreadState_New(sub_interp->interp); PyEval_AcquireThread( tstate ); PyThreadState_Swap( tstate ); PyRun_SimpleString( "import sys" ); // these two lines are just for informational only, and can be removed PyRun_SimpleString( "print (sys.path)" ); PyRun_SimpleString( "import threading" ); // this seems the cause... PyThreadState_Clear( tstate ); PyEval_ReleaseThread( tstate ); PyThreadState_Delete( tstate ); // may execute in another separate sub-thread PyEval_RestoreThread( sub_interp ); Py_EndInterpreter( sub_interp );// we will get the AssertionError (non-harmful?) error msg: Exception ignored in: