Using Ironpython in Unity

Hi, I am trying to embed IronPython inside my Unity application. I encountered this error when I try to run the application. I implemented this chunk of code inside Visual C# console application and it works properly. Does anyone know what's the cause of this error?

Thanks


using UnityEngine;
using System.Collections;

using IronPython;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
using System.IO;
using System;
using System.Text;

public class PythonEngine : MonoBehaviour
{
    ScriptEngine _engine;
    ScriptRuntime _runtime;
    CompiledCode _code;
    ScriptScope _scope;

    void Start(){
        string source = @"d:\Script	estpython.py";
        _engine = Python.CreateEngine();
        _runtime = _engine.Runtime;
        _scope = _engine.CreateScope();

        ScriptSource _script = _engine.CreateScriptSourceFromFile(source, Encoding.ASCII, SourceCodeKind.File);
        _code = _script.Compile(); // error when running this line
    }
}


ArgumentNullException: Argument cannot be null.
Parameter name: textReader

IronPython.Runtime.PythonContext.GetSourceReader (System.IO.Stream stream, System.Text.Encoding defaultEncoding, System.String path) 
Microsoft.Scripting.Runtime.LanguageBoundTextContentProvider.GetReader () 
Microsoft.Scripting.SourceUnit.GetReader () 
IronPython.Compiler.Parser.CreateParserWorker (Microsoft.Scripting.Runtime.CompilerContext context, IronPython.PythonOptions options, Boolean verbatim) 
IronPython.Compiler.Parser.CreateParser (Microsoft.Scripting.Runtime.CompilerContext context, IronPython.PythonOptions options) 
IronPython.Runtime.PythonContext.ParseAndBindAst (Microsoft.Scripting.Runtime.CompilerContext context) 
IronPython.Runtime.PythonContext.CompilePythonCode (Nullable`1 compilationMode, Microsoft.Scripting.SourceUnit sourceUnit, Microsoft.Scripting.CompilerOptions options, Microsoft.Scripting.ErrorSink errorSink) 
IronPython.Runtime.PythonContext.CompileSourceCode (Microsoft.Scripting.SourceUnit sourceUnit, Microsoft.Scripting.CompilerOptions options, Microsoft.Scripting.ErrorSink errorSink) 
Microsoft.Scripting.SourceUnit.Compile (Microsoft.Scripting.CompilerOptions options, Microsoft.Scripting.ErrorSink errorSink) 
Microsoft.Scripting.SourceUnit.Compile (Microsoft.Scripting.ErrorSink errorSink) 
Microsoft.Scripting.Hosting.ScriptSource.CompileInternal (Microsoft.Scripting.CompilerOptions compilerOptions, Microsoft.Scripting.Hosting.ErrorListener errorListener) 
Microsoft.Scripting.Hosting.ScriptSource.Compile () 
(wrapper remoting-invoke-with-check) Microsoft.Scripting.Hosting.ScriptSource:Compile ()
PythonEngine.Start ()   (at Assets\FAME Assets\Scripts\PythonEngine.cs:27   at Microsoft.Scripting.Utils.ContractUtils.RequiresNotNull (System.Object value, System.String paramName) [0x00000])
Microsoft.Scripting.SourceCodeReader..ctor (System.IO.TextReader textReader, System.Text.Encoding encoding) 
(wrapper remoting-invoke-with-check) Microsoft.Scripting.SourceCodeReader:.ctor (System.IO.TextReader,System.Text.Encoding)
Microsoft.Scripting.SourceCodeReader..cctor () 

I'm not familiar with IronPython, but just a guess - does IronPython work with .Net 2.0? I notice on the IronPython website it says:

IronPython also runs on the pre-released .NET 4.0 RC, as well as previous versions of .NET: 3.5, 3.0, and 2.0 SP1.

Note that it says "2.0 SP1", whereas I believe the current version of Unity (2.6) just uses plain v2.0 with no service packs, so if IronPython isn't compatible with this, it could be the cause of your problems.

(The version of .net in Unity is being upgraded in the coming Unity 3.0 release)

Thread bump: Some working example editor code. The basics of hosting python here:

and a simple python interactive interpreter here:

This is editor code - not game code; still the wrong framework for runtime. However it’s handy for editor work.