ironpython - 如何从嵌入式引擎访问IronPython Lib目录?

标签 ironpython embedding

我正在将 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/

相关文章:

visual-studio - 如何使用 IronPython Studio 运行单元测试?

ironpython - Spotfire - 将交叉表中的一行值除以另一行值

gensim - 有没有办法从 KeyedVectors 词汇表中删除单词?

go - 来自嵌入式类型的复合文字和字段

c++ - 如何将 WebKit 嵌入到我的 C/C++/Win32 应用程序中?

java - 定义和重用 MVEL 函数

linq - 您可以在 IronPython 中使用 LINQ 类型和扩展方法吗?

c# - 从 IronPython 调用 C# 函数

c# - 有没有办法将嵌入式 python 脚本作为 python 模块导入到 IronPython 中?

pointers - bytes.Reader,替换底层 []byte 数组