3

在过去的两天里,我一直在努力寻找是否可以在 C# 中运行 python 模块。我感兴趣的模块是 Sympy,可以在这里找到http://code.google.com/p/sympy/(以防有人感兴趣)。这是一个数学库,这个库可以满足我的需要。我还没有找到一个同样好的 C# 库(我测试了 math.net 和其他..)

通过搜索谷歌,我发现您可以使用 IronPython 在 C# 中执行 python 代码。还没有找到任何使用 IronPython 或 PythonNet 模块的真正好例子。

谁能告诉我是否可以在 C# 中使用 python 模块,他会推荐 IronPython 或 PythonNet。另外,如果可能的话,这是否意味着我的 APP 需要安装 python 编译器才能工作,还是引用的 dll 就足够了?

4

2 回答 2

3

我不知道在 c# 中加载 python 库会有多好,但我会通过 IronPython 编写我的程序。

然后,您可以将其编译为 exe,这样您的用户就不会知道其中的区别。

现在这是猜测,但也许您可以在其他 c# 项目中加载您的程序集并以这种方式使用它。即用ironpython包装python库,然后在c#中加载它

我敢打赌,它并不优雅,但它会起作用。

于 2010-01-25T20:02:55.023 回答
1

我知道这是一个老问题,但它可能会帮助某人:)

因此,您可以将您的 python 脚本放入您的 Anaconda3\Lib\site-packages 文件夹,并在他们的 wiki 页面中使用 pythonnet 示例:

static void Main(string[] args)
{
    using (Py.GIL())
    {
        dynamic foo = Py.Import("your_script_name");
    }
}

And the referenced dll should do the work. And for Iron Python i think it's only supported for python 2.x and it's not managed any longer by the authors so that might be a bit complicated. Also you may want to pay attention on your IIS threading issue for Python GIL, it might become a huge pain after a while.

于 2019-03-27T12:47:19.907 回答