windows py2exe 生成dll?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/550446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:58:53  来源:igfitidea点击:

py2exe to generate dlls?

pythonwindowsdllpy2exe

提问by Brian R. Bondy

Is there a way using py2exe or some other method to generate dll files instead of exe files?

有没有办法使用 py2exe 或其他一些方法来生成 dll 文件而不是 exe 文件?

I would want to basically create a normal win32 dll with normal functions but these functions would be coded in python instead of c++.

我基本上想用普通函数创建一个普通的 win32 dll,但这些函数将用 python 而不是 c++ 编码。

采纳答案by Macke

I think you could solve this by doing some hacking:

我认为你可以通过做一些黑客来解决这个问题:

  • Take a look at the zipextimporter module in py2exe . It helps with importing pyd-files from a zip.
  • Using that, you might be able to load py2exe's output file in your own app/dll using raw python-api. (Use boost::python if you can and want)
  • And, since py2exe's outputfile is a zip, you could attach it at the end of your dll, making the whole thing even more integrated. (Old trick that works with jar-files too.)
  • 查看 py2exe 中的 zipextimporter 模块。它有助于从 zip 导入 pyd 文件。
  • 使用它,您可以使用原始 python-api 在您自己的应用程序/dll 中加载 py2exe 的输出文件。(如果可以并且愿意,请使用 boost::python )
  • 而且,由于 py2exe 的输出文件是一个 zip,您可以将它附加到 dll 的末尾,使整个文件更加集成。(也适用于 jar 文件的老技巧。)

Not tested, but I think the theory is sound.

没有经过测试,但我认为这个理论是合理的。

Essentially, you reimplement py2exe's output executable's main() in your dll.

本质上,您在 dll 中重新实现了 py2exe 的输出可执行文件的 main()。

回答by Eli Bendersky

I doubt that py2exe does this, as it's architectured around providing a bootstrapping .exe that rolls out the python interpreter and runs it.

我怀疑 py2exe 是否会这样做,因为它的架构围绕提供一个引导 .exe 来推出 Python 解释器并运行它。

But why not just embed Python in C code, and compile that code as a DLL?

但是为什么不直接将 Python 嵌入 C 代码中,并将该代码编译为 DLL?

回答by Tom Alsberg

I am not aware of py2exebeing 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).

我不知道py2exe能够做到这一点,因为我相信它实际上并没有从 Python 代码中生成对象符号,而只是将编译后的字节码嵌入到带有 Python 运行时的可执行文件中)。

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 Elmerfor that.

创建本机库可能需要使用 Python-C API 做更多工作(定义事物的 C/C++ 接口)。使用Elmer可能会更容易一些。

回答by A.Wallen

For posterity, I was able to use Elmerto 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.

为了后代,我最近能够使用Elmer成功生成一个可用的 DLL。他们的站点有一个构建加载 python 代码的 DLL 包装器的示例。这非常酷,因为您可以动态更改 Python 代码以更改 DLL 行为以进行调试。

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.

不幸的是,对我来说,我想要一个可以在不安装 python 的情况下工作的可移植 DLL。那部分并不是开箱即用的。这里没有重复所有步骤,而是通过我采取的步骤提供答案的链接:https: //stackoverflow.com/a/24811840/3841168。我确实必须分发 python27.dll、elmer.dll 和几个 .pyd 以及我的 .dll;还需要适当的 .net 运行时,因为 python27.dll 通常不是静态链接的。可能有一些方法可以包含大量 dll,但我不介意分发多个 DLL,所以我没有深入研究它。

回答by denfromufa

It looks like it is possible to generate a COM DLL from py2exe:

看起来可以从 py2exe 生成 COM DLL:

http://www.py2exe.org/index.cgi/Py2exeAndCtypesComDllServer

http://www.py2exe.org/index.cgi/Py2exeAndCtypesComDllServer

  23     my_com_server_target = Target(
  24     description = "my com server",
  25     # use module name for ctypes.com dll server
  26     modules = ["dir.my_com_server"],
  27     # the following line embeds the typelib within the dll
  28     other_resources = [("TYPELIB", 1, open(r"dir\my_com_server.tlb", "rb").read())],
  29     # we only want the inproc (dll) server
  30     create_exe = False
  31     )