I’m updating a package to work with Python 3.10. It is a package which loads python interpreter into another process via shared library. Everything works in 3.9 and earlier, but due to changes made in 3.10 existing code, which was calling Py_Main
no longer properly works, i.e sys.prefix
and sys.exec_prefix
aren’t properly intialized when executed in the virtual environment.
So I went with the Python Initialization Configuration method.
When I just call Py_RunMain()
instead of Py_Main()
– everything works as expected, with the exception of parsing of command line arguments.
So I created an (what it seems to be an empty PyConfig
object, into which I copied arguments using PyConfig_SetBytesArgv
. But after doing that, I now no longer get correct sys.prefix and sys.exec_prefix inside of virtualenv.
My pseudo-C code looks as follows now:
int main(int argc, char **argv)
{
PyStatus status;
PyConfig config;
Py_SetProgramName(...);
Py_Initialize();
PyEval_InitThreads();
PyEval_SaveThread();
PyConfig_InitPythonConfig(&config);
status = PyConfig_SetBytesArgv(&config, argc, argv);
if (PyStatus_Exception(status)) {
goto exception;
}
status = Py_InitializeFromConfig(&config);
if (PyStatus_Exception(status)) {
goto exception;
}
PyConfig_Clear(&config);
return Py_RunMain();
exception:
PyConfig_Clear(&config);
if (PyStatus_IsExit(status)) {
return status.exitcode;
}
Py_ExitStatusException(status);
}
What am I missing here?
Thanks!