[IronPython] How to call IronPython Interpreter with C# code?

Jim Hugunin jimhug at exchange.microsoft.com
Fri Jul 22 00:55:49 CEST 2005


This is a good example, but I think that using Execute to load the
assembly by name is a dangerous approach.  I'd strongly suggest loading
your assemblies using the following API - this is likely to be a
standard idiom for making the program's classes available to a script:

engine.LoadAssembly(typeof(Program).Assembly);

In fact, I'd go so far as to suggest the value of adding a variable to
the namespace before running the user's script, something like this:

engine.SetVariable("program", typeof(Program));

My two cents - Jim

-----Original Message-----
From: users-ironpython.com-bounces at lists.ironpython.com
[mailto:users-ironpython.com-bounces at lists.ironpython.com] On Behalf Of
Corey Haines
Sent: Thursday, July 21, 2005 3:11 PM
To: Discussion of IronPython
Subject: Re: [IronPython] How to call IronPython Interpreter with C#
code?

Wow! Thanks, Martin. That actually helps me out quite a bit, as well.
I took a look at the console code to see how it does it, but this puts
it into a much more concise form.

I'd like to use ironpython as the scripting language for my system,
and this will make it much easier.

-Corey

On 7/21/05, Martin Maly <martmaly at exchange.microsoft.com> wrote:
> You can call IronPython through the IronPython.Hosting.PythonEngine
> object.
> Here's a trivial example:
> 
>    using IronPython.Hosting;
>    class Program {
>        static void Main(string[] args) {
>            PythonEngine engine = new PythonEngine();
>            Console.WriteLine( engine.Evaluate("2 + 2") );
>        }
>    }
> 
> To get this to compile, add reference to IronPython.dll
> 
> The other interesting methods on PythonEngine are:
> 
> public void AddToPath(string dirName);          ... adds path to
> sys.path
> public void Execute(string text);
> public object Evaluate(string expr);
> public void ExecuteFile(string fileName);
> public int RunFileInNewModule(string fileName);
> 
> 
> Here is another example, little more interesting, I think. The program
> (running as Embed.exe) will
> execute the Python statement, which in turn will load the Embed's type
> information and modify the static field.
> 
> using System;
> using IronPython.Hosting;
> 
> namespace Embed {
>    public class Program {
>        public static int Value = 0;
> 
>        static void Main(string[] args) {
>            PythonEngine engine = new PythonEngine();
> 
>            engine.Execute(
>                "import sys                         \n" +
>                "sys.LoadAssemblyByName(\"Embed\")  \n"+
>                "import Embed                       \n" +
>                "Embed.Program.Value = 20           \n");
> 
>            Console.WriteLine(Value);
>        }
>    }
> }
> 
> And the last example that goes even further and uses SetVariable
method:
> 
> using System;
> using IronPython.Hosting;
> using System.Windows.Forms;
> 
> namespace Embed {
>    public class Program {
>        static void Main(string[] args) {
>            PythonEngine engine = new PythonEngine();
> 
>            Form form = new Form();
>            engine.SetVariable("form", form);
> 
>            engine.Execute(
>                "import sys                                         \n"
> +
>                "sys.LoadAssemblyByName('System.Windows.Forms')     \n"
> +
>                "from System.Windows.Forms import Button            \n"
> +
>                "def on_exit(*args):                                \n"
> +
>                "    Application.Exit()                             \n"
> +
>                "b = Button(Text='Exit')                            \n"
> +
>                "b.Click += on_exit                                 \n"
> +
>                "form.Controls.Add(b)                               \n"
> +
>                "form.Show()
> \n");
> 
>            Application.Run();
>        }
>    }
> }
> 
> I hope this helps.
> 
> Martin
> 
> -----Original Message-----
> From: users-ironpython.com-bounces at lists.ironpython.com
> [mailto:users-ironpython.com-bounces at lists.ironpython.com] On Behalf
Of
> shhgs
> Sent: Wednesday, July 20, 2005 4:18 PM
> To: users-ironpython.com at lists.ironpython.com
> Subject: [IronPython] How to call IronPython Interpreter with C# code?
> 
> Can somebody tell me how to call IronPython Interpreter with C# code,
so
> that I can embed Python into the .NET application.
> 
> Thank you!
> _______________________________________________
> users-ironpython.com mailing list
> users-ironpython.com at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
_______________________________________________
users-ironpython.com mailing list
users-ironpython.com at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com



More information about the Ironpython-users mailing list