Embedding Python 3.3 in C ++ from the zipped standard library in Windows XP

I want to embed Python 3.3.4 in my C ++ application so that:

  • The Python standard library is always taken from the zip archive along with the application executable (should not depend on any surrounding vars, etc.);
  • my own custom .py modules are imported from another folder or zip archive along with the executable.

And, in fact, I did almost everything right. The only thing that still does not work is to import the standard library from the ZIP archive: it works fine as a simple directory, but whenever I try to zip it up, the initialization fails with the following error:

Fatal Python error: Py_Initialize: unable to load the file system codec

Is this possible with the latest Python? I searched a lot for it, and many sources claim that working on the correct "python33.zip" should work on the executable. However, my experiments prove the opposite. What am I missing?

Here, my test code is a minimal console application created by MS Visual Studio 2010, running on Windows XP SP3, with some comments regarding what I tried and what the results are:

#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;
}
+4
source share
2 answers

, Python 3.3.4. 3.3.2 3.3.3 .

Python:

http://bugs.python.org/issue20852

+1

Source: https://habr.com/ru/post/1530036/


All Articles