py2exe生成dll?
有没有办法使用 py2exe 或其他方法生成 dll 文件而不是 exe 文件?
我想基本上创建一个具有普通函数的普通 win32 dll,但这些函数将用 python 而不是 c++ 编码。
Is there a way using py2exe or some other method to generate dll files instead of exe files?
I would want to basically create a normal win32 dll with normal functions but these functions would be coded in python instead of c++.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为你可以通过一些黑客行为来解决这个问题:
未经测试,但我认为这个理论是合理的。
本质上,您在 dll 中重新实现 py2exe 的输出可执行文件的 main() 。
I think you could solve this by doing some hacking:
Not tested, but I think the theory is sound.
Essentially, you reimplement py2exe's output executable's main() in your dll.
我怀疑 py2exe 会这样做,因为它的架构是围绕提供引导 .exe 来推出 python 解释器并运行它。
但为什么不直接将 Python 嵌入到 C 代码中,并将该代码编译为 DLL 呢?
I doubt that py2exe does this, as it's architectured around providing a bootstrapping .exe that rolls out the python interpreter and runs it.
But why not just embed Python in C code, and compile that code as a DLL?
我不知道 py2exe 能够做到这一点,因为我相信它实际上并没有从 Python 代码中生成对象符号,而只是将编译后的字节码嵌入到带有 Python 的可执行文件中运行)。
使用 Python-C API 创建本机库可能需要更多工作(定义事物的 C/C++ 接口)。 使用 Elmer 可能会更容易一些。
I am not aware of
py2exe
being able to do that, as I believe that it does not actually make object symbols out of your Python code, but just embeds the compiled byte-code in an executable with the Python runtime).Creating a native library may require a bit more work (to define the C/C++ interface to things) with the Python-C API. It may be somewhat easier using Elmer for that.
为了后代,我最近能够使用 Elmer 成功生成可用的 DLL。 他们的网站有一个构建加载 python 代码的 DLL 包装器的示例。 这非常酷,因为您可以即时更改 python 代码来更改 DLL 行为以进行调试。
不幸的是,对我来说,我想要一个无需安装 python 即可运行的可移植 DLL。 那部分并没有完全开箱即用。 不要重复所有步骤,这里是一个指向答案的链接,其中包含我所采取的步骤:https://stackoverflow.com/a/ 24811840/3841168。
我确实必须将 python27.dll、elmer.dll 和几个 .pyd 与我的 .dll 一起分发; 由于 python27.dll 通常不是静态链接的,因此还需要适当的 .net 运行时。 可能有一些方法可以解决包含大量 DLL 的问题,但我不介意分发多个 DLL,因此我没有深入研究它。
For posterity, I was able to use Elmer to successfully generate a usable DLL recently. Their site has an example of building a DLL wrapper that loads python code. It is pretty cool because you can change the python code on the fly to change the DLL behavior for debugging.
Unfortunately, for me, I wanted a portable DLL that would work without installing python. That part didn't didn't quite work out of the box. Rather than repeating all the steps, here is a link to the answer with the steps I took: https://stackoverflow.com/a/24811840/3841168.
I did have to distribute python27.dll, elmer.dll and a couple of .pyd's along with my .dll; an appropriated .net runtime was also needed since the python27.dll is not usually statically linked. There may be some way around including a boatload of dll's, but I didn't mind distributing multiple DLLs, so I didn't dig into it too much.
看起来可以从 py2exe 生成 COM DLL:
http://www.py2exe.org /index.cgi/Py2exeAndCtypesComDllServer
It looks like it is possible to generate a COM DLL from py2exe:
http://www.py2exe.org/index.cgi/Py2exeAndCtypesComDllServer