[IronPython] IP 2.0 Hosting

Dino Viehland dinov at exchange.microsoft.com
Mon Dec 17 23:38:28 CET 2007

My guess here would be that Script is the DLL name but it doesn't include a namespace named Script.  One fairly common issue is that AddReference add's references to the assembly, but import imports types and namespaces from the loaded assemblies.  So if the type doesn't have a namespace you should just import TypeName, and if it's in a namespace then you should import Namespace.

Respond back if that's not the problem.

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Slide
Sent: Monday, December 17, 2007 12:38 PM
To: Discussion of IronPython
Subject: Re: [IronPython] IP 2.0 Hosting

On Dec 17, 2007 9:48 AM, Dino Viehland <dinov at exchange.microsoft.com> wrote:
> Yes, the document is not yet up to date - we're currently working on implementing it so the next release will probably have some of it, and subsequence releases will have the whole thing.
> For giving objects to C# code you need to derive from the interface (and implement the methods).  If you fail to implement any methods we'll throw at runtime when that method is called.
> For exposing classes/namespaces/etc...  I think we may have a deficiency here even in the new model.  While we do have a global namespace per ScriptRuntime I don't believe we have a way to say "populate this with these types/namespaces".  I'll bring this up next time we're going over the spec internally.
> Here's some simple code you can give a shot to get started.  I don't have anything comprehensive, this is just what I pulled from EngineTest.cs which if you get the IronPython source code is in IronPythonTest.  I imagine whatever Toni can post will be more useful :).
> using IronPython.Runtime.Hosting;
> using Microsoft.Scripting;
> ...
> PythonEngine pe = PythonEngine.CurrentEngine;
> pe.Execute(script); # where script is a string w/ some code in it.  This runs in the DefaultModule which is:
> IScriptModule default_module = ScriptDomainManager.CurrentManager.Host.DefaultModule;
> You can also do:
> pe.Execute("x = 1", anonymousModule);
> You can create new modules via:
> ScriptModule publishedModule = pe.CreateModule("published_context_test", ModuleOptions.PublishModule);

Can you give some quick examples of how to import modules from C#? I
am having issues with import exceptions when I try to do things. The
path to the module is definitely within the sys.path, but I get all
sorts of errors. Here is a snippet of my code:


foreach (string assembly in _assemblies)

foreach (KeyValuePair<string, object> obj in _objects)
      __main__.SetVariable(obj.Key, obj.Value);

_script_engine.SystemState.prefix =

_script_engine.Execute("import ScriptHelper");
_script_engine.Execute(string.Format("import {0} as Config",

ScriptHelper.py is just a set of simple python functions for grabbing
information about loaded modules, etc. It resides in the current
working directory (same as the EntryAssembly.Location). My problem
comes from something inside the script (which is written to a temp
file by the UI in my simple test app) importing something (which has
been added using the ClrModule.AddReferenceByPartialName). I get a
'Script' is not defined (with Script being a DLL in the working
directory which has been added as a reference).

Users mailing list
Users at lists.ironpython.com

More information about the Ironpython-users mailing list