Build .NET DLLs from Python code? - python

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.

Related

Compatibility of DLL files for Python C extension

I'm new to Python, and I'm learning to write C extensions for Python under Windows. Following a tutorial, I successfully compiled my exmaple.dll file using Cygwin.
The dll file seems okay as I can import it and the function of it also works.
Note that this is done using the Python of Cygwin. However, I can't use this dll under my own Python (not the one in Cygwin). I have copied the dll file to the Python search path, though. ImportError was raised.
I'm thinking, is it because the versions of two Python are different?
Cygwin comes with Python 2.7.5, while I use 2.7.6.
(Posted on behalf of the OP).
Finally I decided to use Ubuntu, it's simpler to accomplish this.

How to read content of .dll libraries as Python code

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.

Compile DLL from python code

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.

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.

How do I tell which Python interpreter I'm using?

I am using Python 2.5.2. How can I tell whether it is CPython or IronPython or Jython?
Another question: how can I use a DLL developed in VB.NET in my project?
import platform
platform.python_implementation()
The above one provide the type of interpreter you use .
If you downloaded it as the default thing from python.org, it's CPython. That's the “normal” version of Python, the one you get when you use the command “python”, and the one you'll have unless you specifically went looking for the projects targeting Java/CIL.
And it's CPython because neither of the other projects have reached version number 2.5.2.
How can i use a dll developed in VB.NET in to my project?
From CPython, using Python-for-.NET(*) and clr.AddReference.
(*: actually a bit of a misnomer, as with CPython in control it is more like .NET-for-Python.)
If you are typing "python" to launch it, it is probably CPython. IronPython's executable name is "ipy".
import sys
print sys.version
Well since the first part of your question has been answered, I'll tackle the second part. .NET dll's can be accessed from python in various ways. If you are using ironpython it makes this especially easy, since all .NET languages can interact with eachother fairly seamlessly. In this case you would access your dll from ironpython just as you would any other .NET dll you made with ironpython. If you want to call a native dll you can use ctypes.

Categories

Resources