Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

I have a Python module with nothing but regular global functions. I need to call it from another business-domain scripting environment that can only call out to C DLLs. 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? This is for a Windows environment. 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.

like image 596
ApplePieIsGood Avatar asked Mar 14 '09 13:03

ApplePieIsGood


3 Answers

Take a look at this Codeproject article. One way would be wrap your python functions in a C dll and expose this to the callee.

COM is a binary protocol to solve this issue. But you will have to wrap this python dll in a COM wrapper. And add some code on the calling side as well.

like image 174
dirkgently Avatar answered Oct 30 '22 20:10

dirkgently


The standard solution is to embed the Python interpreter (which is already a C DLL) in your application.

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

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

like image 42
S.Lott Avatar answered Oct 30 '22 20:10

S.Lott


Py2exe can generate COM dlls from python code, by compiling and embedding python code + interpreter. It does not, AFAIK, support regular DLLs yet. For that, see dirkgently's answer about embedding python yourself.

like image 37
Macke Avatar answered Oct 30 '22 22:10

Macke