Compile DLL from python code - python

Hi does anyone know if you can compile Python code into a Windows DLL file? How would you go about doing this?

One way would be to create a C or C++ library that embeds the Python interpreter and runs your Python code.
Another option would be to translate you Python code to C++ with ShedSkin and make that a DLL, although I wouldn't expect a very clean API to come out of this.

Related

compile python script for matlab use

I would like to know if I am going in the right direction.
I wrote a script in python and was able to run it through Matlab.
Now I would like to make it available for potential other Matlab users who do not have python installed.
I wanted to make sure that I can do such a thing by compiling my python file.
Also, the compilation in python does not seem straightforward (for example, making a dll How to compile a Python package to a dll extending python with C) and potentially, there are some other solutions to my problem that I would be happy to investigate.
thank you for your help
ps : if it matters, my python script is using TensorFlow, sklearn, numpy, scipy, pandas

pybind with boost/dll - dual use DLL?

TL;DR
Adding pybind11 bindings to a working C++ DLL project allows me to import and use the resulting DLL in Python but breaks my ability to use it in C++ code using boost/dll machinery.
Summary
I've got a C++ library that I compile to FooLib.dll. I use boost/dll's BOOST_DLL_ALIAS and boost::dll::import_alias() to export and load a class Foo that does some work in other C++ code.
Some details omitted but it all works great, following this recipe.
I'd like to be able to call the same library code from Python to do some complicated functional testing and do comparisons to numpy/scipy prototypes without having to write so much test code in C++.
So I tried to add pybind11 bindings to the FooLib DLL project using PYBIND11_MODULE.
It compiles, I get a FooLib.dll. I can copy and rename that to FooLib.pyd, import it as a Python module, and it all works fine. I export Foo as a Python class, and it works.
However, when I compile in the pybind bindings, the boost/dll import machinery can no longer load the original FooLib.dll. I verify with boost::dll::library_info() that the appropriate CreateFoo symbol is exported to the DLL. But loading with boost::dll::import_alias() fails with:
boost::dll::shared_library::load() failed: The specified module could not be found
Minimal Example
Unfortunately, something that needs a C++ and Python executable and compiled boost isn't exactly minimal, but I did my best here:
https://github.com/danzimmerman/pybind-boostdll-minimal
Direct links to the source files:
DLL Project Files
HelloSayerLib.h
HelloSayerImp.cpp
C++ Test Code
HelloSayerLibCppTest.cpp
Python Test Code
HelloSayerLibPythonTests.py
Any advice for next steps?
Is it even possible to compile to one binary that works for both C++ and Python like this?
The suggestion in #n.'pronouns'm. comment is correct. Simply copying the python DLL from the Anaconda distribution I built against to the C++ program's run directory fixes the problem. Makes sense in retrospect, but didn't occur to me.
Makes it even more likely that I should keep the builds separate or at least set up my real project to only build with pybind bindings on my machine.

Compile python scripts with C++ project

I have a C++ project that invokes the same function in each python script. But each script does very different things that need access to the C++ project's internal classes.
So I need a python wrapper so I pass a C++ object to the python script and I need a way to run the python script's function from the C++ project too.
From what I understand with Cython and Shed Skin they are utilities to make C++ classes into a python class but not necessarily share run time objects back and forth between the languages.
What can I do?
Thank you Maverick for your question.
Cython is not a way of using C++ classes into python.Cython is simply python with c data types.Means that you can use c data types in your python program which in turn turns your python code to execute faster.Also you can build extensions in python using cython.
Now comming to the problem in the question,you can use boost library for your task.In boost you just need to write a wrapper to use your c++ object in python or vice versa.You just need to compile it to a shared library which can be done by tools like jam,cython etc.I am not going into details of how you are going to do it as you can find many tutorials for the same.

Build .NET DLLs from Python code?

How do I make a DLL (.NET) written in python code (IronPython)?
You cannot create a standard .NET .dll from IronPython code (.dll that can be used directly from C# or VB).
pyc.py produces .dll that can be used only by IronPython - check such .dll with Reflector and you will understand why.
You can probably use ironpycompiler, using examples in http://pythonhosted.org//ironpycompiler/html-en/command-line.html. It requires installations of both IronPython and of CPython (the regular Python).
You can use the script at C:\Program Files\[IronPython Program Directory]\Tools\Scripts.

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

Categories

Resources