[IronPython] Embedding IronPython and calling python functions from C#

Lepisto, Stephen P stephen.p.lepisto at intel.com
Thu Jun 11 18:34:25 CEST 2009


What I do when I want to work with python modules from embedded IronPython is set the IronPython search path with the path of the module to load then  create and execute a small python script that loads the main python modules into the current scope.

string moduleName = Path.GetFileName(modulePath);
string path = Path.GetDirectoryName(modulePath);

ICollection<string> paths = _pythonEngine.GetSearchPaths();
if (!paths.Contains(path))
{
    paths.Add(path);
    _pythonEngine.SetSearchPaths(paths);
}
string modInvoke = String.Format("import {0}\nfrom {0} import *\n", moduleName);
ScriptSource source = _pythonEngine.CreateScriptSourceFromString(modInvoke,
                                                                 Microsoft.Scripting.SourceCodeKind.Statements);

Where modulePath is the full path to the python module or package to load.

I can then invoke methods or access attributes using the IronPython scope.  In this way, I can interact with the python modules for as long as necessary before closing down the scope/engine.


From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Dody Gunawinata
Sent: Thursday, June 11, 2009 9:09 AM
To: Discussion of IronPython
Subject: Re: [IronPython] Embedding IronPython and calling python functions from C#

You can read all yours file scripts, then using StringBuilder to combine then and call CreateScriptSourceFromString()
Then you can call your functions within the combined scripts normally. Pretty much you are creating a giant source code on the fly.

On Thu, Jun 11, 2009 at 5:32 PM, Patrick van der Willik <patrick at toolmaker.nl<mailto:patrick at toolmaker.nl>> wrote:
I'm currently attempting to embed the IronPython 2 runtimes into an existing application written in C#. However, I find the amount of documentation lacking on what I'm trying to do. I currently have a proof-of-concept version which uses Lua and LuaInterface, but the people who have to write the scripts dislike Lua(Well, more hate it with a passion) and would love to see this working with Python.

My host application is a networked application that must trigger certain scripts functions on events generated by the connected clients. The idea is that when my application starts, it will load the IronPython script environment, launches an 'autoexec.py' which will load various other scripts files and do some housekeeping. Once this all is completed, it will start listening to incoming connections. However, in various scenarios, the application has to trigger scripted functions when data is received from a client. Which script function is called is different per client and per event. I have events for connecting, logging on, disconnecting and a set of data specific events after receiving data. This highly depends on the received packets.

My question here is: How do I embed IronPython in such a fashion that I can load my scripts and then trigger various functions within that? I've seen many examples that just call CreateScriptSourceFromString() or File each time in which just 1 piece of code is implemented. This is not suitable for the needs here because the scripted systems can become quite complex.

With regards,
Patrick

_______________________________________________
Users mailing list
Users at lists.ironpython.com<mailto:Users at lists.ironpython.com>
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com



--
nomadlife.org<http://nomadlife.org>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20090611/d460b210/attachment.html>


More information about the Ironpython-users mailing list