我正在将 IronPython 嵌入到 C# 程序中。我知道我需要使用搜索路径列表调用 ScriptEngine.SetSearchPaths ,以便我的 Python 代码可以导入模块。特别是我必须让它知道标准库模块在哪里,在我的例子中是在这里:
C:\Program Files (x86)\IronPython 2.0.2\Lib
我的问题是,如何以编程方式找到该路径?
这显然是可以完成的,因为 IronPython 控制台知道它:
>>> import sys
>>> sys.path
['C:\\Windows\\system32', 'C:\\Program Files (x86)\\IronPython 2.0.2\\Lib', 'C:\\Program Files (x86)\\IronPython 2.0.2', 'C:\\Program Files (x86)\\IronPython 2.0.2\\lib\\site-packages']
最佳答案
ipy.exe 根据启动进程的 EXE 计算此值:
Assembly entryAssembly = Assembly.GetEntryAssembly();
// Can be null if called from unmanaged code (VS integration scenario)
if (entryAssembly != null) {
string entry = Path.GetDirectoryName(entryAssembly.Location);
string lib = Path.Combine(entry, "Lib");
...
您还可以查看 IronPython.dll 的位置:
Assembly entryAssembly = typeof(Hosting.Python).Assembly;
关于ironpython - 如何从嵌入式引擎访问IronPython Lib目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1392273/