Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to embed PyPy into a .NET application?

I would like to embed a Python interpreter into my .NET application. I'm aware of IronPython, of course, but I'm specifically interested in PyPy, because of its stackless support and microthreads.

However, while PyPy can be built against the CLI, it looks like that just gives you a standalone Python interpreter a la python.exe. I haven't been able to find any documentation for building something that can actually be embedded inside a .NET host application.

Is there a way to use (stackless) PyPy to run Python scripts from a .NET app, and allow those scripts to interact with CLR objects provided by the host application?

like image 502
Joe White Avatar asked May 21 '11 21:05

Joe White


People also ask

How do I use PyPy instead of Python?

For Python 2.7, it's just called pypy . For CPython, if you would like to run Python 3 from the terminal, you simply enter the command python3 . To run PyPy, simply issue the command pypy3 . Entering the pypy3 command in the terminal might return the Command 'pypy3' not found message, as shown in the next figure.

Is PyPy faster than Python?

PyPy vs. On the average, PyPy speeds up Python by about 7.6 times, with some tasks accelerated 50 times or more. The CPython interpreter simply doesn't perform the same kinds of optimizations as PyPy, and probably never will, since that is not one of its design goals.

Is PyPy compatible?

Compatibility: PyPy is highly compatible with existing python code. It supports cffi, cppyy, and can run popular python libraries like twisted, and django. It can also run NumPy, Scikit-learn and more via a c-extension compatibility layer.

How is PyPy implemented?

Internally, PyPy uses a technique known as meta-tracing, which transforms an interpreter into a tracing just-in-time compiler. Since interpreters are usually easier to write than compilers, but run slower, this technique can make it easier to produce efficient implementations of programming languages.


2 Answers

PyPy's CLI backend is not as mature as C backend and also does not integrate as well with .NET libraries. While normal PyPy compiled to C is production ready, I wouldn't call the .NET version production ready. It's also missing the JIT (although some work has been done in this area) and microthreads. Unless someone steps in, IronPython seems to be the only viable option as of now.

like image 105
fijal Avatar answered Oct 07 '22 05:10

fijal


No, there's not. CPython had the ability to access .NET libraries using the Python for .NET (see http://pythonnet.github.io/) maintained at github, but aside from IronPython there's never been a way to actually embed a Python interpreter into a .NET application. This was one of its main selling points.

On a related note, IronPython (by default) has a smaller stack size than CPython when it comes to recursion. That is, you must pass a "-X:FullFrames" command-line option to ipy.exe to enable CPython-esque stack frames. Know this isn't as good as PyPy...but it might help:)

like image 38
Dave Avatar answered Oct 07 '22 05:10

Dave