I want to embed Python 3.3.4 in my C++ application so that:
And, in fact, I've almost managed to do it right. The only thing that still does not work is importing the standard library from a ZIP archive: it works ok as a simple directory, but whenever I try to zip it, initialization fails with the following error:
Fatal Python error: Py_Initialize: unable to load the file system codec
Is it even possible with latest Python? I've googled a lot for it and lots of sources claim that putting correct "python33.zip" near the executable should work. Still, my experiments prove otherwise. What am I missing?
Here's my test code - a minimal console application made by MS Visual Studio 2010, running on Windows XP SP3, with some comments as to what I tried and what are the results:
#include "stdafx.h"
#include "python.h"
int _tmain(int argc, _TCHAR* argv[])
{
// calling or not calling Py_SetProgramName doesn't seem to change anything
//Py_SetProgramName(argv[0]);
// python_lib is a directory with contents of python33/Lib
// python_lib.zip is an equivalent ZIP archive with contents of python33/Lib (without any top-level subdirs)
// _scripts.dat is a ZIP archive containing a custom script (hello.py)
//Py_SetPath(L"python_lib;_scripts.dat"); // works fine! (non-zipped standard library, zipped custom script)
Py_SetPath(L"python_lib.zip;_scripts.dat"); // both std library and scripts are zipped - fails with error "unable to load the file system codec" during Py_Initialize()
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print('Today is',ctime(time()))\n");
PyRun_SimpleString("import hello"); // runs hello.py from inside _scripts.dat (works fine if Py_Initialize succeeds)
Py_Finalize();
return 0;
}
This problem was recently discovered and documented in Python Issue 20621. A fix for it will be released in Python 3.3.5; 3.3.5 release candidate 2 is now available for testing. http://www.python.org/download/releases/3.3.5/
Turned out to be some problem with Python 3.3.4. Installing 3.3.2 or 3.3.3 instead fixes everything instantly.
Filed this issue on Python's bug tracker:
http://bugs.python.org/issue20852
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With