IronPython ScriptRuntime equivalent to CPython PYTHONPATH

匿名 (未验证) 提交于 2019-12-03 01:10:02

问题:

The following import works inside ipy.exe prompt but fails using IronPython ScriptRuntime inside a C# 4.0 program.

import ConfigParser 

C# code:

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 uses PYTHONPATH environment variable. How do I configure this in IronPython when using ScriptRuntime?

回答1:

You want to use GetSearchPaths and SetSearchPaths on your engine object. You could parse the env variable of your choice and populate the search path when you initialize your engine. For example:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!