[IronPython] Access functions from other modules in an embedded environment

Curt Hagenlocher curt at hagenlocher.org
Tue Jul 13 17:59:23 CEST 2010


The easiest way to import a file as a module is with ScriptRuntime.UseFile.

On Mon, Jul 12, 2010 at 2:51 PM, Danny Fernandez <fernandez.dan at gmail.com>wrote:

> Hi I am planning on using IronPython 2.6.1 .NET 2.0 in a embedded in my C#
> app. I am playing with able
> to call python functions from C#. I had a question about how do you call
> other functions from modules
> that were imported. Here is a simple example
>
> moduleA.py has the following
>
> import moduleB
>
> def myFunA():
>  return 1
>
>
> moduleB.py has the following
>
> def myFunB():
>  return 2
>
> This is what I have in my sample C# program.
>
> ScriptSource source = _engine.CreateScriptSourceFromFile("moduleA);
> CompiledCode compiledCode = source.Compile();
> ScriptScope scope = _engine.CreateScope();
> compiledCode.Execute(scope);
>
>
> I can get access to myFunA straight forward with
>
> Func<int> funcA = scope.GetVariable<Func<int>>("myFunA");
>
>
> How do I access other functions from other modules in this simple case
> myFunB from the import moduleB?
> I was able to with the following but I am not sure if this is correct way
> or if there are better way
>
> PythonModule stuff = scope.GetVariable<PythonModule>("moduleB");
> Func<int> funcB= _engine.Operations.GetMember<Func<int>>(stuff, "myFunB");
>
>
> Thanks
>
> Danny
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20100713/f99aed41/attachment.html>


More information about the Ironpython-users mailing list