# IronPython: Embedding in C# - Not fully understanding

## Recommended Posts

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

##### Share on other sites
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.

##### Share on other sites
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

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

src.Execute(scope)?

##### Share on other sites
Quote:
 Original post by ToolmakerProblem 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.

##### Share on other sites
Quote:
 Original post by arbitusThe 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.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628372
• Total Posts
2982305

• 10
• 9
• 13
• 24
• 11