[IronPython] Access to current Python engine in C# (Silverlight)

Michael Foord fuzzyman at voidspace.org.uk
Mon May 18 09:27:01 PDT 2009


Ha - dammit, no.

With Silverlight this still throws an ImportError when embedded Python 
code executed from C# like this tries to import from the xap file. :-(

Michael

Michael Foord wrote:
> Thanks to some help from William Reade, this code *seems* to work 
> fine. I need to try it from Silverlight and check the Python code it 
> contains is able to import:
>
> using System;
> using System.Collections.Generic;
> using System.Text;
>
>
> using IronPython.Hosting;
> using IronPython.Runtime;
> using IronPython.Runtime.Types;
>
> using Microsoft.Scripting;
> using Microsoft.Scripting.Hosting;
> using Microsoft.Scripting.Runtime;
>
>
> namespace ExecutePython
> {
>    public class ExecutePython
>    {
>        static string code = @"
> class Foo(object):
>    attribute = 'weeeee'
>
> ";
>        public static Scope CreateModule(CodeContext context)
>        {
>            PythonContext python = PythonContext.GetContext(context);
>
>            PythonDictionary globals = new PythonDictionary();
>            globals["__name__"] = "AModule";
>            Scope module = new Scope(globals);
>            SourceUnit script = python.CreateSnippet(code, 
> SourceCodeKind.Statements);
>            script.Execute(module);
>            return module;
>        }
>    }
> }
>
>
> All the best,
>
>
> Michael
>
>
> Michael Foord wrote:
>> Jimmy - did you get a chance to look at this?
>>
>> If the code shown below *genuinely* gets a reference to the current 
>> engine then shouldn't the search path be setup already?
>>
>> Can you see what is wrong with the code below?
>>
>> Thanks
>>
>> Michael
>>
>> Michael Foord wrote:
>>> Hello guys,
>>>
>>> I have a second use case for embedding IronPython in Silverlight. 
>>> This is actually a dynamic application with a C# component that 
>>> needs to programattically build a Python module.
>>>
>>> Again I have the same problem - imports in Python code fail. I would 
>>> have expected that accessing the current runtime and fetching a 
>>> Python engine would fetch the current Python engine, with the 
>>> browser host correctly setup. Unfortunately that seems not to be the 
>>> case. Can anyone spot problems with the following code:
>>>
>>>
>>> using Microsoft.Scripting.Silverlight;
>>> using IronPython;
>>> using IronPython.Hosting;
>>> using Microsoft.Scripting;
>>> using Microsoft.Scripting.Hosting;
>>>
>>> namespace EmbeddedSLModule
>>> {
>>>    public class EmbeddedSLModule
>>>    {
>>>        private static string source = @"
>>> import something
>>> ";
>>>        public static ScriptScope GetModule(){
>>>            ScriptRuntime runtime = DynamicApplication.Current.Runtime;
>>>            ScriptEngine engine = runtime.GetEngine("Python");
>>>            ScriptScope scope = engine.CreateScope();
>>>            ScriptSource script = 
>>> engine.CreateScriptSourceFromString(source, SourceCodeKind.Statements);
>>>            script.Execute(scope);
>>>
>>>            return scope;
>>>
>>>        }
>>>    }
>>> }
>>>
>>>
>>> It works fine for code that doesn't import anything - but imports 
>>> from within the xap file fail.
>>>
>>> Thanks
>>>
>>> Michael Foord
>>>
>>
>>
>
>


-- 
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog




More information about the Users mailing list