Python Forum

Full Version: Py_Initialize Crash with Python 3.6
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
We have had Python 2.7 embedded in our software for some time. We have been going through the process of embedding Python 3.6. We have always relied on the user to be responsible for the installation of the full version of Python but this can have issues. We previously had an issue with 2.7 where if you called Py_Initialize and had no full version of Python installed then you encountered a Python fatal error. We got round this by using code similar to this:

Py_NoSiteFlag = 1;
NCString PyPrefix(Py_GetPrefix()); // This is just a native class of ours similar to CString
if (!PyPrefix.IsEmpty())
{
// If we have got this far then this indicates a full version of Python is present.
// This reinitialize after resetting the NoSiteFlag.
Py_Finalize();
Py_NoSiteFlag = 0;
Py_Initialize();
}

However after our switch to 3.6 the Python fatal error returns as it appears this NoSiteFlag fix doesn't work as you get a different Python fatal error in the first initialize call. I built the debug version of Python 3.6 and stepped into the Py_Initialize function. The fatal error now occurs in the following code:

if (initfsencoding(interp) < 0)
Py_FatalError("Py_Initialize: unable to load the file system codec");

This is because it cannot find a Python Lib folder I believe so cannot load the encodings. Looking at the code there doesn't appear to be any flags that can be set to avoid this.

I am looking for a solution where Py_Initialize doesn't fatal error on Python 3.6 when there isn't a full version of Python present. Pity the initialize function just doesn't return an error rather than throwing a fatal error then we could handle locally. I have read about having a embedded Python kit available locally if there isn't a full version present but we must be able to override this with a full version if one is installed. Any help on this would be appreciated.