views:

351

answers:

2

I am hosting Python in a SL app. Basically i have an attached property that lets you pass Python script which then gets executed on the lost focus event of the element.

The IPY integration works fine as long as I don't need any 3rd party imports. I copied the formencode package to the site-packages folder under Program Files\Iron Python... and then tried to import formencode both programatically using ScriptEngine.ImportModule and inline. In each case the module is not found.

My setup code for the DLR host is the following:

        var setup = new ScriptRuntimeSetup();
        setup.HostType = typeof(BrowserScriptHost);
        setup.LanguageSetups.Add(Python.CreateLanguageSetup(null));
        setup.Options["SearchPaths"] = new string[] { string.Empty };

        _runtime = new ScriptRuntime(setup);
        _engine = _runtime.GetEngine("Python");
        _scope = _engine.CreateScope();
        _engine.ImportModule("formencode");

Any help would be appreciated.

+1  A: 

I haven't tried to do exactly this, but I did find that to include 3rd party assemblies (farseer physics engine in my case) I had to include the assembly in the XAP and reference it in the App.manifest.

But the thing that really caught me was I had to reference the assembly with its strong name (which I only had to do in SL, locally I could just use the assembly name).

Hope this helps and I'll be interested to see how you do end up resolving it.

tarn
+1  A: 

The DLR Hosting code you have looks good, but where you put formencode is wrong:

I copied the formencode package to the site-packages folder under Program Files\Iron Python

the setup.HostType = typeof(BrowserScriptHost) line causes IronPython to look in the XAP file for all file-system operations, including "import". You will need to copy formencode.py into your host's Silverlight project, and make sure "Copy Local" is set to "True" so it makes it's way into the XAP. Then importing it will work.

This is expected because Silverlight cannot read from any location on your operating system's file system, as it needs to work on everyone's machine :)

Jimmy Schementi
Thanks Jimmy, I will try that. Know of any easy way to get the files added to the XAP? Doing it manually is a real pain.
Glenn Block
Chiron.exe is the tool of choice; it will create a XAP file out of any folder.
Jimmy Schementi