IronPython: Embedding in C# - Not fully understanding

Started by
4 comments, last by Toolmaker 14 years, 10 months ago
I'm currently trying to embed IronPython within a C# environment. I'm most likely going to use it to replace Lua that I currently use(with LuaInterface). The background: I currently use Lua and after creating the LuaVM, I load an autoexec.lua which loads various other files through an include into the VM. My application starts doing it's thing and on certain events, it'll call a function that's written in Lua through the VM. This works fine. I'm trying to accomplish the same thing with IronPython at the moment. I want to create a new ScriptEngine, expose a few functions to it(Such as an 'include' or similar command), load my autoexec.py and have that include various other files. I'm up to a point where I can load an autoexec.py(or just inject other code), but I'm not entirely sure how to proceed from here. First of all, how do I call a function in the SE instance? This is what I go up till now:

            engine = Python.CreateEngine();

            var code = "def add(a, b): return a + b";
            var src = engine.CreateScriptSourceFromString(code, Microsoft.Scripting.SourceCodeKind.InteractiveCode);
            
            var scope = engine.CreateScope();
            // How do I call add?

Ideas? Toolmaker

Advertisement
I'm afraid I can't answer your question directly, but the upcoming .NET 4.0/C# 4.0 adds support for the DLR via the dynamic keyword. If jumping ahead a little into VS 2010 (currently available as a public beta) is an option that may make life considerably easier.

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

Problem is: I don't want to upgrade to a BETA for the moment for this. While it is an option, it should be possible to embed IronPython without using the 4.0 framework.

I find it rather odd that the IronPython documentation on this is so lacking. I do want to use the IronPython that's compliant up to Python 2.5 but the amount of documentation on it is so lacking. Esp. for a Microsoft project.

Toolmaker

Python people generally won't help you with embedding - only extending. ;) That may account for lack of docs.

src.Execute(scope)?
Quote:Original post by Toolmaker
Problem is: I don't want to upgrade to a BETA for the moment for this. While it is an option, it should be possible to embed IronPython without using the 4.0 framework.

I find it rather odd that the IronPython documentation on this is so lacking. I do want to use the IronPython that's compliant up to Python 2.5 but the amount of documentation on it is so lacking. Esp. for a Microsoft project.

Toolmaker


The book IronPython in Action by Manning Press has a couple chapters on this and provides a simple engine that will allow for embedding IronPython. Overall it is a simple process, but you have to understand the mechanics behind it.

It works just fine in .NET 3.5.
Quote:Original post by arbitus
The book IronPython in Action by Manning Press has a couple chapters on this and provides a simple engine that will allow for embedding IronPython. Overall it is a simple process, but you have to understand the mechanics behind it.

It works just fine in .NET 3.5.


Well, that's the problem. The documentation lacks for it, and I'm actually looking for information on the web / some background info without having to pay for it. I can read code, and understand it, but none of the code I've seen till so far actually was useful or explains the mechanics behind it.

This topic is closed to new replies.

Advertisement