代码之家  ›  专栏  ›  技术社区  ›  Rodrigo Strauss

IronPython ScriptRuntime相当于CPython Python

  •  7
  • Rodrigo Strauss  · 技术社区  · 16 年前

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

    import ConfigParser
    

    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 回复  |  直到 16 年前
        1
  •  14
  •   Tom E    16 年前

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

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