Incorporación de IronPython, comando de ayuda integrado, mis objetos CLR

I'm embedding IronPython (2.6.1) in a C# assembly and exposing several objects to scripts which are executed with PythonEngine.ExecuteFile. I expose them either with

scope.SetVariable("SomeObject", new SomeObject())


engine.Execute("from MyNamespace import SomeObject", scope)

depending on how the scripts use them. My application assembly is added to the engine with


Now a script can execute help(SomeObject) and dump the nice little help info(*), however it's incomplete. None of the object's events or properties (public of course) show up and many of the 'built-in' members are missing as well.

Here's the odd part; If I fire up ipy.exe and execute the following:

import sys
sys.path.append('<location of my app>')
import clr
from MyNamespace import SomeObject

I get a different dump, complete with all the missing members!

Why do the two differ?

Pregunta extra: Assuming I get it working correctly, is it possible to add descriptive text on my CLR objects to the output of help()? Like you can from within the script, on your python-native types? My first guess was the DescriptionAttribute, but that didn't work.

(*) Obviously a final, working script wouldn't do this but it is exceedingly helpful while writing/testing the script.


Here is a complete console program that illustrates how to import the site which replaces the usless internal help() with the standard python library help().

using System;
using System.Collections.Generic;
using System.Reflection;
using IronPython.Hosting;
using IronPython.Runtime;
using Microsoft.Scripting.Hosting.Providers;

namespace MyApp
    class Program
        static void Main(string[] args)
            // Work around issue w/ pydoc - piping to more doesn't work so instead indicate that we're a dumb terminal
            if (Environment.GetEnvironmentVariable("TERM") == null)
                Environment.SetEnvironmentVariable("TERM", "dumb");

            var engine = Python.CreateEngine();

            // Add standard Python library path (is there a better way to do this??)
            PythonContext context = HostingHelpers.GetLanguageContext(engine) as PythonContext;
            ICollection<string> paths = context.GetSearchPaths();
            paths.Add(@"C:\Program Files (x86)\IronPython 2.6\Lib");

            // Import site module


            var scope = engine.CreateScope();
            scope.SetVariable("SomeObject", new SomeObject());
            engine.Execute("help(SomeObject)", scope);

    /// <summary>
    /// Description of SomeObject.
    /// </summary>
    public class SomeObject
        /// <summary>
        /// Description of SomeProperty.
        /// </summary>
        public int SomeProperty { get; set; }
        /// <summary>
        /// Description of SomeMethod.
        /// </summary>
        public void SomeMethod() { }
        /// <summary>
        /// Description of SomeEvent.
        /// </summary>
        public event EventHandler SomeEvent;

preguntado el 08 de enero de 11 a las 19:01

1 Respuestas

My guess is that in your app you're not importing the standard library. IronPython includes a built-in help function and the standard library includes a help function which gets installed by If you make sure the standard library is available when you host and then import when you create the engine then you should get the standard library help. That being said it's probably a bug or missing feature that the built-in help isn't documenting events and properties.

Regarding the documentation - yes, you just need to use C#'s doc comments and build with the /doc:MyAssemblyName.xml option. If the XML file is in the same directory as the assembly IronPython will read the doc strings and provide them for doctor attributes which help() then reads.

Respondido el 09 de enero de 11 a las 05:01

I was able to verify that this answer is correct, however I am not (so far) able to figure out how to import programatically. My verification consisted of building ipy.exe (it's all of one simple .cs file) and trying the ipy procedure above. I get the broken help results. If I then make the standard python library available to this new ipy.exe, I get the correct help results. So the solution to loading the standard library is buried deep in the bowls of Microsoft.Scripting.Hosting or IronPython.Hosting. OO-Spaghetti would be a kind word for that mess. - Tergiver

I figured out how to get the site module loaded and updated my question above. - Tergiver

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.