IronPython ScriptRuntime相当于CPython PYTHONPATH

时间:2009-11-18 12:10:08

标签: ironpython c#-4.0

以下导入在ipy.exe提示符内有效,但在C#4.0程序中使用IronPython ScriptRuntime失败。

import ConfigParser

C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;

namespace CSharpDynamic
{
    class Program
    {
        static int Main(string[] args)
        {
            ScriptRuntime python = Python.CreateRuntime();
            dynamic dynamicIni =
python.UseFile(@"c:\test\WebCast\DynamicIni.py");

            return 0;
        }
    }
}

CPython使用PYTHONPATH环境变量。使用ScriptRuntime时如何在IronPython中配置它?

1 个答案:

答案 0 :(得分:14)

您想在引擎对象上使用GetSearchPaths和SetSearchPaths。您可以解析所选的env变量,并在初始化引擎时填充搜索路径。例如:

var engine = Python.CreateEngine(DefaultEngineOptions());
var paths = engine.GetSearchPaths();
paths.Add("c:\\my_libs");
engine.SetSearchPaths(paths);