简体   繁体   English

在Windows XP上从压缩标准库嵌入C ++中的Python 3.3

[英]Embedding Python 3.3 in C++ from zipped standard library on Windows XP

I want to embed Python 3.3.4 in my C++ application so that: 我想在我的C ++应用程序中嵌入Python 3.3.4,以便:

  • Python's standard library is always taken from a zip archive alongside my app's executable (shouldn't depend on any environment vars etc); Python的标准库总是从我的应用程序的可执行文件中取出一个zip存档(不应该依赖于任何环境变量等);
  • my own custom .py modules are imported from another folder or zip archive alongside the executable. 我自己的自定义.py模块从可执行文件旁边的另一个文件夹 zip存档导入。

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: 唯一仍然不起作用的是从ZIP存档导入标准库:它可以作为一个简单的目录工作,但每当我尝试压缩它时,初始化失败并出现以下错误:

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

Is it even possible with latest Python? 甚至可能使用最新的Python? I've googled a lot for it and lots of sources claim that putting correct "python33.zip" near the executable should work. 我已经搜索了很多东西,很多消息来源声称在可执行文件附近放置正确的“python33.zip”应该可行。 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: 这是我的测试代码 - 由MS Visual Studio 2010制作的最小控制台应用程序,在Windows XP SP3上运行,并对我尝试的内容和结果有一些评论:

#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 . 最近在Python Issue 20621中发现并记录了这个问题。 A fix for it will be released in Python 3.3.5; 它的修复程序将在Python 3.3.5中发布; 3.3.5 release candidate 2 is now available for testing. 3.3.5发布候选人2现在可用于测试。 http://www.python.org/download/releases/3.3.5/ http://www.python.org/download/releases/3.3.5/

Turned out to be some problem with Python 3.3.4. 原来是Python 3.3.4的一些问题。 Installing 3.3.2 or 3.3.3 instead fixes everything instantly. 安装3.3.2或3.3.3而是立即修复所有内容。

Filed this issue on Python's bug tracker: 在Python的bug跟踪器上提到了这个问题:

http://bugs.python.org/issue20852 http://bugs.python.org/issue20852

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM