带有 Python 嵌入的 C++:如果未安装 Python,则会崩溃

C++ with Python embedding: crash if Python not installed(带有 Python 嵌入的 C++:如果未安装 Python,则会崩溃)

本文介绍了带有 Python 嵌入的 C++:如果未安装 Python,则会崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 上开发,我到处搜索,但没有找到任何人谈论这种事情.

I'm developing on Windows, and I've searched everywhere without finding anyone talking about this kind of thing.

我在我的桌面上制作了一个 C++ 应用程序,它使用 MSVC 嵌入了 Python 3.1.我链接了 python31.lib 并在应用程序的运行文件夹中包含了 python31.dll 和可执行文件.它工作得很好.我的扩展和嵌入代码绝对有效,没有崩溃.

I made a C++ app on my desktop that embedded Python 3.1 using MSVC. I linked python31.lib and included python31.dll in the app's run folder alongside the executable. It works great. My extension and embedding code definitely works and there are no crashes.

我将运行文件夹发送给我没有安装 Python 的朋友,但在脚本设置阶段应用程序崩溃了.

I sent the run folder to my friend who doesn't have Python installed, and the app crashes for him during the scripting setup phase.

几个小时前,我在安装了 Python 2.6 的笔记本电脑上试用了该应用程序.我和我朋友遇到了同样的崩溃行为,通过调试发现是 Py_Initialize() 调用失败了.

A few hours ago, I tried the app on my laptop that has Python 2.6 installed. I got the same crash behavior as my friend, and through debugging found that it was the Py_Initialize() call that fails.

我在笔记本电脑上安装了 Python 3.1,但没有更改应用程序代码.我运行它,它运行完美.我卸载了 Python 3.1,应用程序再次崩溃.我在我的应用程序中放入代码以从本地 python31.dll 动态链接,以确保它正在使用它,但我仍然崩溃.

I installed Python 3.1 on my laptop without changing the app code. I ran it and it runs perfectly. I uninstalled Python 3.1 and the app crashes again. I put in code in my app to dynamically link from the local python31.dll, to ensure that it was using it, but I still get the crash.

我不知道解释器是否需要比DLL更多的东西来启动或者什么.我一直无法找到有关此的任何资源.Python 文档和其他指南似乎从未解决如何在不让用户在本地安装 Python 的情况下分发使用 Python 嵌入的 C/C++ 应用程序.我知道这在 Windows 上比在 Unix 上更像是一个问题,但我见过许多在本地嵌入 Python 的 Windows C/C++ 应用程序,但我不确定它们是如何做到的.

I don't know if the interpreter needs more than the DLL to start up or what. I haven't been able to find any resources on this. The Python documentation and other guides do not seem to ever address how to distribute your C/C++ applications that use Python embedding without having the users install Python locally. I know it's more of an issue on Windows than on Unix, but I've seen a number of Windows C/C++ applications that embed Python locally and I'm not sure how they do it.

除了 DLL 之外,我还需要什么?为什么当我安装 Python 时它可以工作,然后在我卸载它时停止工作?听起来它应该如此微不足道;也许这就是为什么没有人真正谈论它.尽管如此,我无法真正解释如何处理这个崩溃问题.

What else do I need other than the DLL? Why does it work when I install Python and then stop working when I uninstall it? It sounds like it should be so trivial; maybe that's why nobody really talks about it. Nevertheless, I can't really explain how to deal with this crash issue.

非常感谢您.

推荐答案

除了pythonxy.dll,还需要整个Python库,即lib文件夹的内容,加上扩展模块,即DLL 文件夹.如果没有标准库,Python 甚至无法启动,因为它会尝试查找 os.py(在 3.x 中;在 2.x 中为 string.py).在启动时,它会导入许多模块,特别是 site.py.

In addition to pythonxy.dll, you also need the entire Python library, i.e. the contents of the lib folder, plus the extension modules, i.e. the contents of the DLLs folder. Without the standard library, Python won't even start, since it tries to find os.py (in 3.x; string.py in 2.x). On startup, it imports a number of modules, in particular site.py.

它搜索标准库的位置有很多;在您的情况下,它最终会在注册表中找到它.之前,使用可执行名称(通过 Py_SetProgramName 设置)试图找到地标;它还检查文件 python31.zip,它应该是标准库的压缩副本.它还检查环境变量 PYTHONHOME.

There are various locations where it searches for the standard library; in your cases, it eventually finds it in the registry. Before, uses the executable name (as set through Py_SetProgramName) trying to find the landmark; it also checks for a file python31.zip which should be a zipped copy of the standard library. It also checks for a environment variable PYTHONHOME.

您可以自由地从不需要的东西中剥离库;有多种工具可以静态计算依赖项(特别是 modulefinder).

You are free to strip the library from stuff that you don't need; there are various tools that compute dependencies statically (modulefinder in particular).

如果你想尽量减少文件数量,你可以

If you want to minimize the number of files, you can

  1. 将所有扩展模块静态链接到您的 pythonxy.dll,甚至将 pythonxy.dll 静态链接到您的应用程序
  2. 使用冻结工具;这将允许将标准库的字节码链接到您的 pythonxy.dll.
  3. (替代 2.)将 pythonxy.zip 用于标准库.

这篇关于带有 Python 嵌入的 C++:如果未安装 Python,则会崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:带有 Python 嵌入的 C++:如果未安装 Python,则会崩溃

基础教程推荐