1

我正在尝试从 C# 运行 IronPython 程序跟踪模块,但我似乎无法使其工作。

我的 C# 代码:

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

namespace PythonTest
{
   static class Program
   {
      static void Main(String[] args)
      {
         ScriptEngine pyEngine = Python.CreateEngine();
         ScriptScope pyScope = pyEngine.CreateScope();

         pyEngine.ExecuteFile(args[0], pyScope);
      }
   }
}

对于应用程序,我传递了这个 python 脚本的路径:

import sys

#adding the path to the python modules
sys.path.append(r'c:/Program Files (x86)/IronPython 2.7/Lib')

#the code to be traced
s = """
def mooo(n):
   if n == 0:
      return 1
   else:
      return n * mooo(n-1)

print(mooo(6))"""

#make the tracer
import trace
tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix], trace=1, count=1)

#run the tracer
tracer.run(s)

#write the results
r = tracer.results()
r.write_results(show_missing=True, coverdir='c:/Temp')

运行 C# 应用程序时,我从跟踪模块中抛出异常:

Unhandled Exception: IronPython.Runtime.Exceptions.ImportException: No module na
med __main__
   at Microsoft.Scripting.Runtime.LightExceptions.CheckAndThrow(Object value)
   at Microsoft.Scripting.Interpreter.FuncCallInstruction`2.Run(InterpretedFrame
 frame)
   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.LightLambda.Run3[T0,T1,T2,TRet](T0 arg0, T
1 arg1, T2 arg2)
   at IronPython.Compiler.PythonCallTargets.OriginalCallTarget2(PythonFunction f
unction, Object arg0, Object arg1)
   at IronPython.Runtime.FunctionCaller`2.Call2(CallSite site, CodeContext conte
xt, Object func, T0 arg0, T1 arg1)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute4[T0,T1,T2,T3,TRet](CallSit
e site, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
   at IronPython.Runtime.FunctionCaller`2.Call2(CallSite site, CodeContext conte
xt, Object func, T0 arg0, T1 arg1)
   at IronPython.Runtime.Method.MethodBinding`1.SelfTarget(CallSite site, CodeCo
ntext context, Object target, T0 arg0)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite s
ite, T0 arg0, T1 arg1, T2 arg2)
   at Microsoft.Scripting.Interpreter.DynamicInstruction`4.Run(InterpretedFrame
frame)
   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 a
rg1)
   at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
   at IronPython.Compiler.PythonScriptCode.Run(Scope scope)
   at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
   at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope)
   at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink)
   at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)
   at Microsoft.Scripting.Hosting.ScriptEngine.ExecuteFile(String path, ScriptSc
ope scope)
   at PythonTest.Program.Main(String[] args) in c:\work\vs projects\PythonTest\P
ythonTest\Program.cs:line 14
Press any key to continue . . .

知道我做错了什么吗?

4

0 回答 0