I am developing some visualization apps using an open-source framework called Omegalib:
https://code.google.com/p/omegalib/
This framework was originally written in C++ but has since incorporated Python versions of the C++ libraries. All of the pre-compiled python modules for Omegalib are provided as dynamic link libraries.
I would love to add some code-completion to my development environment as well as some stronger debugging capabilities than IDLE. In order to do this I need to provide the Python Omegalib libraries to whatever IDE I am using.
I've asked the developer if he knows how to achieve this and according to him, "...The reason is that omegalib python modules are not written in python, but in C++, and since I'm using an embedded interpreter there is no easy way (that I'm aware of, at least) to read the Python version of the modules".
So does anyone know how I can read in the .dll module files as Python code?
Thank you!
You can use ctypes to interface with a library if it has a c api.
However if it only has a C++ api, your only options are with a c extension module. Now there is cython to help with that which supports c and c++. It makes c extensions easier to write via python-like language.
Related
I have a C++ application that uses machine learning from Python and my current approach is making a single file executable with pyInstaller and then just running it from C++. This have obvious drawbacks, notably interapplication communication. At the moment I'm using an intermediate JSON file to talk to each other but this is massively suboptimal for my future requirements. What's beautiful about this, is that is working on all major platforms without too much hassle.
Section 1.6. from Python's manual reads "Compiling and Linking under Unix-like systems"
Does this mean that Python interpreter will be inside my application binary and target system doesn't need to have Python installed as the program will always use embedded interpreter ? If so, whats with python libraries ? Can I embed a whole conda enviroment ?
Also, whats with:
"(...) under Unix-like systems"
Does this means this approach is not multiplatform ?
Thanks in advance.
Embedding the Python interpreter is possible on all platforms. However it will only be the interpreter. Embedding any libraries will be a lot harder or even impossible.
But since you seem to deploy the Python libs already, you can use them just fine from the embedded interpreter. And then you could bridge C++ and Python without IPC, since they are both running in the same process.
pybind11 is very nice for embedding and generating C++ <-> Python interfaces.
A possible alternative, depending on the libraries in use, may be to export the model and use a C++ library to load and use it (for instance Tensorflow -> ONNX -> ONNX runtime).
It means that cpython (Python interpreter) will be inside your application. You will be able to run Python code and observe and manipulate virtual machine state directly from C++ code (good entry point C API reference is here). Your application might have some additional dynamic library dependencies (which ones depends on compilation options of embedded Python). Also interpreter isn't completely self contained and depends on some external .py modules normally shipped with Python distribution (as standard library). If you plan to import external modules that expect standard library, you will have to ship it with your application. There are ways how to build modules into binary too (freeze) but you might run into issues specially with modules that rely on filesystem.
As far as I tried, this procedure works on UNIX like systems and Windows (where easiest way is to link against DLL which you then ship with your application). On Windows you also need to make sure that you compile with same compiler as was used to compile DLL (or you compile Python DLL from source). Here is additional information about embedding on Windows: https://docs.python.org/3/faq/windows.html#how-can-i-embed-python-into-a-windows-application
Just note that embedding Python and shipping 3rd party modules with your application might have some licensing consequences.
I have a C++ DLL that is distributed as part of a free utility. While I have found the source code, it is not distributed publicly, and I have not tried building it (not familiar with C++, code is legacy and sits in an old VCS, etc.). Ideally, I would like to use this DLL from Python without relying on header or source files.
Is this possible using Boost.Python or SWIG (or something else)? If so, can you point me to an example or resources that shows how? I initially tried using ctypes, but I learned that it cannot bind C++ classes. Some Googling suggested these alternatives, but I have yet to see how to do it without having to compile the C++ source from their tutorials.
Thank you!
I have a .cpp and .h source file pair which is a manager (I guess a wrapper also) for a c++ library I have made. I want to let people use this manager to work with my library in python. I have heard about several different ways to wrap this library into python like cython and boost.python but Im having trouble with understanding the process.
If I want to make this manager usable in python, do I need to wrap it in a different way for each version of python? (2.7 vs 3.4) Do I also need to wrap it in a different way for each operating system for each version? So 2.7/3.4 for Windows vs 2.7/3.4 for Linux?
Concerning your confusion about the process, just follow any tutorial for any of the wrapper libs you found or where suggested in the comments.
If I want to make this manager usable in python, do I need to wrap it in a different way for each version of python? (2.7 vs 3.4)
Yes. You might be able to load binary modules compiled for Python 3.4 into Python 3.5, but it's unlikely to work across major versions.
Do I also need to wrap it in a different way for each operating system for each version?
Yes. Just as you need to compile your C++ code for different operating systems (and possibly versions) and CPU architectures, Python modules are not different. However, the "wrap it in a different way" just means "compile for the target environment".
I've successfully embedded Python in a multi-platform C++ project.
This required linking to a libpython, which needs to be provided for each platform I'm targeting. For OSX it was easy, I just pulled it out of some homebrew folder.
But I would like my Python scripts to use imports from the standard library (e.g. this one)
What is that going to involve?
Standard Library documentation for Python 3 says that the standard library is a mix of compiled units and .py files, so I'm expecting I will have to maybe link my project against a second library, and somehow inform the Python runtime of the location of the folder containing the standard library's .py files.
But is it really going to be this simple? Is this process documented anywhere?
Am I going to run into trouble on mobile platforms? It looks as though Kivy might be on their way towards solving this problem...
I have a third-party product, a terminal emulator, which provides a DLL that can be linked to a C program to basically automate the driving of this product (send keystrokes, detect what's on the screen and so forth).
I want to drive it from a scripting language (I'm comfortable with Python and slightly less so with Perl) so that we don't have to compile and send out executables to our customers whenever there's a problem found.
We also want the customers to be able to write their own scripts using ours as baselines and they won't entertain the idea of writing and compiling C code.
What's a good way of getting Python/Perl to interface to a Windows DLL. My first thought was to write a server program and have a Python script communicate with it via TCP but there's got to be an easier solution.
One way to call C libraries from Python is to use ctypes:
>>> from ctypes import *
>>> windll.user32.MessageBoxA(None, "Hello world", "ctypes", 0);
In Perl, Win32::API is an easy way to some interfacing to DLLs. There is also Inline::C, if you have access to a compiler and the windows headers.
Perl XSUBs can also create an interface between Perl and C.
In Perl, P5NCI will also do that, at least in some cases. But it seems to me that anything you use that directly manages interfacing with the dll is going to be user-unfriendly, and if you are going to have a user (scriptor?) friendly wrapper, it might as well be an XS module.
I guess I don't see a meaningful distinction between "compile and send out executables" and "compile and send out scripts".
For Python, you could compile an extension which links to the DLL, so that in Python you could just import it like a normal module. You could do this by hand, by using a library like Boost.Python, or by using a tool such as SWIG (which also supports Perl and other scripting languages) to generate a wrapper automatically.
The Python Py_InitModule API function allows you to create a module from c/c++ functions which can then be call from Python.
It takes about a dozen or so lines of c/c++ code to achieve but it is pretty easy code to write:
https://python.readthedocs.org/en/v2.7.2/extending/extending.html#the-module-s-method-table-and-initialization-function
The Zeus editor that I wrote, uses this appoach to allow Zeus macros to be written in Python and it works very well.