cdi*_*ins 5 c# scripting ironpython
当使用IronPython托管时,__name__特殊变量将返回<module>而不是__main__.我在这里找到了一些讨论:http://lists.ironpython.com/pipermail/users-ironpython.com/2006-August/003274.html.但我不知道如何将它应用于我的代码:
public static void RunPythonFile(string filename)
{
// This is the Key to making sure that Visual Studio can debug
// the Python script. This way you can attach to 3dsMax.exe
// and catch exceptions that occur right at the correct location
// in the Python script.
var options = new Dictionary<string, object>();
options["Debug"] = true;
// Create an instance of the Python run-time
var runtime = Python.CreateRuntime(options);
// Retrive the Python scripting engine
var engine = Python.GetEngine(runtime);
// Get the directory of the file
var dir = Path.GetDirectoryName(filename);
// Make sure that the local paths are available.
var paths = engine.GetSearchPaths();
paths.Add(dir);
engine.SetSearchPaths(paths);
// Execute the file
engine.ExecuteFile(filename);
}
Run Code Online (Sandbox Code Playgroud)
您需要使用托管API中的更多低级功能:
ScriptEngine engine = Python.CreateEngine();
ScriptScope mainScope = engine.CreateScope();
ScriptSource scriptSource = engine.CreateScriptSourceFromFile("test.py", Encoding.Default, SourceCodeKind.File);
PythonCompilerOptions pco = (PythonCompilerOptions) engine.GetCompilerOptions(mainScope);
pco.ModuleName = "__main__";
pco.Module |= ModuleOptions.Initialize;
CompiledCode compiled = scriptSource.Compile(pco);
compiled.Execute(mainScope);
Run Code Online (Sandbox Code Playgroud)
取自http://mail.python.org/pipermail/ironpython-users/2011-November/015419.html.
| 归档时间: |
|
| 查看次数: |
1134 次 |
| 最近记录: |