简体   繁体   English

有没有办法从Python模块构建类似C的DLL?

[英]Is there a way to build a C-like DLL from a Python module?

I have a Python module with nothing but regular global functions. 我有一个Python模块,只有常规的全局函数。 I need to call it from another business-domain scripting environment that can only call out to C DLLs. 我需要从另一个只能调用C DLL的业务域脚本环境中调用它。 Is there anyway to build my Python modules so that to other code it can be called like a standard C function that's exported from a DLL? 反正有没有建立我的Python模块,以便其他代码可以像从DLL导出的标准C函数一样调用它? This is for a Windows environment. 这适用于Windows环境。 I'm aware of IronPython, but as far as I know it can only build .NET Assemblies, which are not callable as C DLL functions. 我知道IronPython,但据我所知它只能构建.NET程序集,它不能作为C DLL函数调用。

Take a look at this Codeproject article. 看看这个 Codeproject文章。 One way would be wrap your python functions in a C dll and expose this to the callee. 一种方法是将您的python函数包装在C dll中并将其公开给被调用者。

COM is a binary protocol to solve this issue. COM是解决此问题的二进制协议。 But you will have to wrap this python dll in a COM wrapper. 但是你必须将这个python dll包装在一个COM包装器中。 And add some code on the calling side as well. 并在主叫方面添加一些代码。

The standard solution is to embed the Python interpreter (which is already a C DLL) in your application. 标准解决方案是在应用程序中嵌入Python解释器(已经是C DLL)。

https://docs.python.org/extending/windows.html#using-dlls-in-practice https://docs.python.org/extending/windows.html#using-dlls-in-practice

http://docs.python.org/extending/embedding.html http://docs.python.org/extending/embedding.html

Py2exe can generate COM dlls from python code, by compiling and embedding python code + interpreter. Py2exe可以通过编译和嵌入python代码+解释器从python代码生成COM dll。 It does not, AFAIK, support regular DLLs yet. 它没有,AFAIK,还支持常规DLL。 For that, see dirkgently's answer about embedding python yourself. 为此,请参阅dirkgently关于自己嵌入python的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM