[IronPython] Embedding.

Martin Maly martmaly at exchange.microsoft.com
Mon Jul 11 16:10:03 PDT 2005


Sorry for the delay in responding, Alan.

For the simplest scenarios you don't need to initialize PythonEngine.
For example, consider:

class Program {
    static void Main(string[] args) {
        PythonEngine e = new PythonEngine();
        object r = e.Evaluate("2+2");    //r == 4
    }
}

For the more complicated cases, initialization is often necessary (for
example to execute scripts that import modules and need sys.path
initialized etc. You can refer to PythonCommandLine.cs to see how the
IronPythonConsole initializes the PythonEngine. Essentially, we set up
things

1) The sys.path variable (via PythonEngine.AddToPath method)
2) further setup of sys module (via PythonEngine.InitializeModules
method)
    - this sets up sys.version, sys.prefix, sys.executable and
sys.exec_prefix attributes
3) import site.py (via PythonEngine.ImportSite method)

The interactive initialization is slightly different, but essentially
this is all that is needed to get IronPython engine initialized.

Hope this helps. Let me know if other questions arise.
Martin

> Alan Kennedy Wrote:
> 
> Greetings all,
> 
> I've worked a lot with jython in the past, and have often 
> made use of it by embedding it in java servers.
> 
> I'd like to try the same thing with Ironpython.
> 
> Is there any documentation on how to go about this? I'm 
> specifically interested in writing ironpython "servlets", 
> i.e. servicing web requests with ironpython code.
> 
> Under jython, the interpreter has to be initialised before 
> scripts can be executed. Is a similar initialisation 
> necessary under ironpython?
> 
> Thanks in advance,
> 
> Regards,
> 
> Alan.


More information about the users-ironpython.com mailing list