在Windows上嵌入Python:为什么它必须是一个DLL? [英] Embedding Python on Windows: why does it have to be a DLL?

查看:139
本文介绍了在Windows上嵌入Python:为什么它必须是一个DLL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写嵌入Python的软件插件。在Windows上,插件技术上是一个DLL(这可能是相关的)。 Python Windows常见问题解答说:



< blockquote>

1.Do not 将Python直接构建到.exe文件中。在Windows上,Python必须是一个DLL来处理本身是DLL的导入模块。 (这是第一个关键的无证件事实。)而是链接到pythonNN.dll;它通常安装在C:\Windows\System中。 NN是Python版本,一些数字,例如Python 2.3的23。


我的问题是为什么Python必须是一个DLL ?如果在我的情况下,主机应用程序不是.exe,而且是一个DLL,我可以在其中构建Python吗?或者也许这个注释意味着第三方C扩展依赖于 pythonN.N.dll 来存在,而其他的DLL不会这样做?假设我真的想要一个DLL,我该怎么办?



我看到有$ code> dynload_win.c 文件,它似乎是在Windows上导入C扩展的模块,据我所见,它扫描扩展文件以查找哪个 pythonX.X .dll 它导入;但我没有Windows的经验,我不太明白所有的代码。

解决方案

您需要链接到 pythonXY.dll 作为DLL,而不是将相关代码直接链接到可执行文件中,因为否则Python运行时无法加载其他 DLL它所依赖的扩展模块)。如果您自己制作DLL,那么理论上可以直接链接该DLL中的所有Python代码,因为它不会在可执行文件中停留,但仍然在DLL中。但是,您必须注意正确地进行链接,因为几乎所有标准工具(如distutils)都不会为您做到这一点。



但是,无论嵌入Python如何,您不能使用只是该DLL,也不能做任何DLL。 ABI在Python版本之间发生变化,所以如果你根据Python 2.6编译代码,你需要 python26.dll ;您不能使用 python25.dll python27.dll 。而Python不仅仅是一个DLL;它还需要其标准库,其中包括扩展模块(这些DLL本身,虽然它们有 .pyd 扩展名)。 dynload_win中的代码。 c 你遇到的是加载这些 DLL,并且与 pythonXY.dll 的加载无关。 p>

简而言之,为了将Python嵌入到插件中,您需要使用插件运送Python,或者要求已经安装了正确的Python版本。


I'm trying to write a software plug-in that embeds Python. On Windows the plug-in is technically a DLL (this may be relevant). The Python Windows FAQ says:

1.Do not build Python into your .exe file directly. On Windows, Python must be a DLL to handle importing modules that are themselves DLL’s. (This is the first key undocumented fact.) Instead, link to pythonNN.dll; it is typically installed in C:\Windows\System. NN is the Python version, a number such as "23" for Python 2.3.

My question is why exactly Python must be a DLL? If, as in my case, the host application is not an .exe, but also a DLL, could I build Python into it? Or, perhaps, this note means that third-party C extensions rely on pythonN.N.dll to be present and other DLL won't do? Assuming that I'd really want to have a single DLL, what should I do?

I see there's the dynload_win.c file, which appears to be the module to import C extensions on Windows and, as far as I can see, it scans the extension file to find which pythonX.X.dll it imports; but I'm not experienced with Windows and I don't quite understand all the code there.

解决方案

You need to link to pythonXY.dll as a DLL, instead of linking the relevant code directly into your executable, because otherwise the Python runtime can't load other DLLs (the extension modules it relies on.) If you make your own DLL you could theoretically link all the Python code in that DLL directly, since it doesn't end up in the executable but still in a DLL. You'll have to take care to do the linking correctly, however, as pretty much none of the standard tools (like distutils) will do this for you.

However, regardless of how you embed Python, you can't make do with just the DLL, nor can you make do with just any DLL. The ABI changes between Python versions, so if you compiled your code against Python 2.6, you need python26.dll; you can't use python25.dll or python27.dll. And Python isn't just a DLL; it also needs its standard library, which includes extension modules (which are DLLs themselves, although they have the .pyd extension.) The code in dynload_win.c you ran into is for loading those DLLs, and are not related to loading of pythonXY.dll.

In short, in order to embed Python in your plugin, you need to either ship Python with the plugin, or require that the right Python version is already installed.

这篇关于在Windows上嵌入Python:为什么它必须是一个DLL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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