C ++与Python嵌入:崩溃如果Python未安装 [英] C++ with Python embedding: crash if Python not installed

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

问题描述

我在Windows上开发,我在所有地方搜索,没有找到任何人谈论这种事情。

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

我在桌面上创建了一个C ++应用程序嵌入式Python 3.1使用MSVC。我链接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嵌入的C / C ++应用程序,而无需用户在本地安装Python。我知道这是更多的问题在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文件夹的内容,加上扩展模块,即DLLs文件夹的内容。没有标准库,Python甚至不会启动,因为它试图找到os.py(在3.x中; string.py在2.x中)。在启动时,它会导入一些模块,特别是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作为标准库。

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆