python - 从 Windows XP 上的压缩标准库将 Python 3.3 嵌入到 C++ 中

标签 python c++ windows python-3.x windows-xp

我想在我的 C++ 应用程序中嵌入 Python 3.3.4,以便:

  • Python 的标准库始终与我的应用程序的可执行文件一起从 zip 存档中获取(不应依赖于任何环境变量等);
  • 我自己的自定义 .py 模块是从可执行文件旁边的另一个文件夹 zip 存档导入的。

而且,事实上,我几乎已经做到了。唯一仍然不起作用的是从 ZIP 存档导入标准库:它作为一个简单的目录工作正常,但每当我尝试压缩它时,初始化失败并出现以下错误:

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

最新的 Python 甚至有可能吗?我在谷歌上搜索了很多,很多消息来源声称在可执行文件附近放置正确的“python33.zip”应该有效。尽管如此,我的实验证明并非如此。我错过了什么?

这是我的测试代码 - 一个由 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;
}

最佳答案

此问题最近被发现并记录在 Python Issue 20621 中.它的修复将在 Python 3.3.5 中发布; 3.3.5 候选版本 2 现已可供测试。 http://www.python.org/download/releases/3.3.5/

关于python - 从 Windows XP 上的压缩标准库将 Python 3.3 嵌入到 C++ 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22174760/

相关文章:

python - 如何删除列值为 NaN 的行的百分比

windows - 优雅地终止从 "start/b"的 Windows 批处理文件启动的 java (jetty/solr) 进程

windows - Docker 在安装 Android 模拟器后停止在 Windows 10 上运行

windows - 带有依赖服务的定时任务重启服务

python - 如何使用非唯一的 bin 边缘进行 qcut?

python - Pandas GroupBy.agg() 抛出 TypeError : aggregate() missing 1 required positional argument: 'arg'

javascript - 列表中的喜欢按钮,按下效果始终位于第一个

c++ - 如何从由 google HEAPPROFILER 创建的 .heap 文件生成图表

c++ - 从 float C++ 中提取数字

c++ - std::isnan 尚未声明,带有 g++-5