I wrote a library (dll) in c++ that uses an embedded Python interpreter. Executing Python scripts in the embedded interpreter works fine when the library is used by a c++ program.
If I call the functions of the dll from a Python program I run into trouble. When the dll would usually start the embedded interpreter by calling Py_Initialize() there is an interpreter running already (I can test this by calling Py_IsInitialized() ). When I try to use this interpreter the program crashes.
Minimal example that reproduces the behavior:
Code for dll:
#include "stdafx.h"
#include <Python.h>
extern "C" __declspec(dllexport)
int testIsInitialized()
{
return Py_IsInitialized();
}
extern "C" __declspec(dllexport)
void testRunSimpleString()
{
PyRun_SimpleString("print('test')");
}
Python script:
import ctypes
dll=ctypes.windll.LoadLibrary('pytest.dll')
# test if Python interpreter is initialized
print(dll.testIsInitialized())
# test if we can run a simple Python script
dll.testRunSimpleString()
Output:
1
Traceback (most recent call last):
File "test_dll.py", line 9, in <module>
dll.testRunSimpleString()
OSError: exception: access violation reading 0x0000000000000010
My main question is: How can I execute Python code from a c++ library that is imported by a different Python program?
UPDATE
The example works when I aquire the global interpreter lock (GIL) in my testRunSImpleString() function. I do not understand why this is necessary here.