[IronPython] Embedded IronPython 2.6 Module Name

Curt Hagenlocher curt at hagenlocher.org
Thu Nov 5 19:45:19 CET 2009

I'm not sure if there's a simpler way -- here's what I did a few months back
when I needed to publish something via __main__:

pythonContext = HostingHelpers.GetLanguageContext(self._engine)
module = pythonContext.CreateModule()
pythonContext.PublishModule('__main__', module)
scope = HostingHelpers.CreateScriptScope(self._engine, module.Scope)
scope.SetVariable('__name__', '__main__')
scope.SetVariable('__doc__', '')

HostingHelpers is a class in the DLR hosting interfaces. From C#, you'd need
to explicitly cast the LanguageContext to a PythonContext.

After this, you would run your Python code within the "scope" ScriptScope.
On Thu, Nov 5, 2009 at 10:57 AM, Jonathan Howard <jhoward at drawloop.com>wrote:

> Hi,
> We're trying to upgrade to IronPython 2.6 and are having a number of issues
> with it.  The biggest issue is there doesn't appear to be a way to name the
> module that runs when you embed your code.  We're using the Hosting API to
> create and run code within C#.  The code runs, but we have other modules
> that are supposed to pull globals from the __main__ module.  This worked in
> 1.1 by setting the DefaultModule on the PythonEngine instance.  There
> doesn't seem to be a way to do this anymore.
> Any help would be appreciated.
> ~Jonathan
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20091105/48d8e52e/attachment.html>

More information about the Ironpython-users mailing list