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?
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.
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.
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.
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.
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.
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:)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With