我正在尝试让我的一些 Python 脚本可以从 .NET 调用。我发现 IronPython 是完成此任务的好工具。所以我尝试使用它,但现在我有以下问题:
- 如何将包安装到 IronPython?我现有的 Python 代码使用它们,所以我需要安装它们。我试着关注 this guide安装 pip,但得到
Process is terminated due to StackOverflowException.并且还没有找到解决问题的方法。 - 假设我将能够安装包,是否仍然不能在 IronPython 中使用具有 C/C++ 绑定(bind)的包?因为在 the FAQ page上面写着不能安装NumPy和SciPy,但是页面好像比较旧。
- 如何从其他 .NET 代码访问 Python 代码? The documentation page说我必须使用 DLR Hosting API,因为从 IronPython 编译的程序集不符合 CLS。但我不知道如何让它与包一起工作。
- 也许还有另一种从 .NET 调用 Python 代码的方法?
提前致谢。
请您参考如下方法:
好的,我想我已经想通了。
- pip 的问题是因为我尝试使用 ipy64。使用ipy解决了这个问题。
- 是的,今天不可能使用带有 C/C++ 绑定(bind)的包。
假设 pip 和请求是使用
ipy -X:Frames -m ensurepip和ipy -X:Frames -m pip install requests安装的(更多关于 IronPython 中 pip 的信息可以在 here 中找到)。可以通过以下方式使用 DLR 托管 API 使用请求:var engine = IronPython.Hosting.Python.CreateEngine(new Dictionary<string, object> {{"Frames", true}}); var searchPaths = engine.GetSearchPaths(); searchPaths.Add(@"C:\Program Files (x86)\IronPython 2.7\Lib"); searchPaths.Add(@"C:\Program Files (x86)\IronPython 2.7\Lib\site-packages"); engine.SetSearchPaths(searchPaths); engine.CreateScriptSourceFromString("import requests; r = requests.get('http://www.google.com/'); print r.status_code").Execute();有关托管 Python 的更多信息可在托管 IronPython 部分下的 IronPython 文档文件(默认位置为
C:\Program Files (x86)\IronPython 2.7\Doc\IronPython.chm)中找到.- As denfromufa has suggested可以使用 Python for .NET 从 .NET 调用 CPython 代码。
