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

Michael Foord fuzzyman at voidspace.org.uk
Fri Jul 17 23:24:27 CEST 2009


Jimmy Schementi wrote:
> Michael,
>
> Oh, sorry to leave you hanging. Here's a SL3 VS solution which imports a python file from C#: http://jimmy.schementi.com/silverlight/foord-repro.zip
>   

Thanks - the goal is actually to import a Python module using the 
*current* Python engine without creating a new one (hence the use of the 
PythonContext).

Thanks

Michael

> // App.xaml.cs
> public class EmbeddedSLModule {
>         public static ScriptScope GetModule() {
>             var pyfile = "toimport.py";
>             ScriptEngine engine = new ScriptRuntime(DynamicEngine.CreateRuntimeSetup(true)).GetEngine("IronPython");
>             var code = new XapVirtualFilesystem().GetFileContents(pyfile);
>             ScriptScope scope = engine.CreateScope();
>             ScriptSource script = engine.CreateScriptSourceFromString(code, pyfile);
>             script.Execute(scope);
>             return scope;
>         }
> }
>
> <!-- MainPage.xaml -->
> <UserControl x:Class="foord_repro.MainPage"
>     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" >
>   <Grid x:Name="LayoutRoot">
>         <TextBlock x:Name="Message" />
>   </Grid>
> </UserControl>
>
> # toimport.py
> import something
>
> # something.py
> import clr
> clr.AddReference("System.Windows")
> from System.Windows import Application
> Application.Current.RootVisual.FindName("Message").Text = "Hi from IronPython!"
>
> The binaries are the latest in the IronPython codeplex source tree, but are also included in the zip.
>
> For the "CreateModule" example, where does that have to do with importing from the XAP file? If you give me an example working on the desktop, I'll see if any modifications need to be made to work in SL, but if this doesn't work in SL it would seem to be a bug in IronPython.
>
> ~js
>
>   
>> -----Original Message-----
>> From: users-bounces at lists.ironpython.com [mailto:users-
>> bounces at lists.ironpython.com] On Behalf Of Michael Foord
>> Sent: Friday, July 17, 2009 5:34 AM
>> To: Discussion of IronPython
>> Subject: Re: [IronPython] Access to current Python engine in C# (Silverlight)
>>
>> I never got a reply to this. The C# code below throws an import error and I
>> want to know if this is my fault or a problem with the IronPython / Silverlight
>> integration.
>>
>> Michael
>>
>> Michael Foord wrote:
>>     
>>> 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/
>>
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>     
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>   


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





More information about the Ironpython-users mailing list