在 C# 中运行 python 模块?
过去两天我一直在绞尽脑汁试图找出是否可以在 C# 中运行 python 模块。我感兴趣的模块是 Sympy,可以在这里找到 http://code.google.com /p/sympy/(如果有人感兴趣的话)。这是一个数学库,这个库可以满足我的需要。我还没有找到一个同样好的 C# 库(我测试了 math.net 和其他库。)
通过搜索 google,我发现你可以使用 IronPython 在 C# 中执行 python 代码。还没有找到任何将模块与 IronPython 或 PythonNet 结合使用的真正好的示例。
谁能告诉我是否可以在 C# 中使用 python 模块以及他会推荐 IronPython 或 PythonNet。另外,如果可能的话,这是否意味着我的应用程序需要安装 python 编译器才能工作,或者引用的 dll 就足够了?
I've been banging my head for the past two days trying to find out if it's possible to run a python module in C#. The module I'm interested in is Sympy which can be found here http://code.google.com/p/sympy/ (in case anyone's interested). It's a math library, and this one does what I need it to do. I haven't found a C# library which is as good (I tested math.net and others..)
From searching google I found out that you can execute python code inside C# using IronPython. Haven't found any real good example of using a module with IronPython or with PythonNet.
Can anyone tell me if it's possible to use a python module in C# and what would he recommend IronPython or PythonNet. Also if it's possible, does it mean my APP will need the python compiler installed to work or will the referenced dll be enough?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道在 C# 中加载 python 库效果如何,但我会通过 IronPython 编写我的程序。
然后您可以将其编译为 exe,这样您的用户就不会知道其中的区别。
现在这是猜测,但也许您可以在其他 C# 项目中加载程序集并以这种方式使用它。即用ironpython 包装python 库,然后在c# 中加载它
,这并不优雅,但我敢打赌它会起作用。
I don't know how well loading a python lib in c# will do ya, but I'd write my program via IronPython.
you can then compile it to an exe so your users don't know the difference.
now this is guesswork, but maybe you can then load your assembly in other c# projects and use it that way. i.e. wrap the python library with ironpython and then load that in c#
its not elegant, but it'll work, I bet.
我知道这是一个老问题,但它可能会对某人有所帮助:)
因此,您可以将 python 脚本放入 Anaconda3\Lib\site-packages 文件夹中,并在其 wiki 页面中使用 pythonnet 示例:
并且引用的 dll 应该可以完成工作。
对于 Iron Python,我认为它仅支持 python 2.x,并且作者不再对其进行管理,因此可能有点复杂。
另外,您可能需要注意 Python GIL 的 IIS 线程问题,一段时间后它可能会变得非常痛苦。
I know its an old question, but it might help someone :)
So you can place your python script into your Anaconda3\Lib\site-packages folder and using the pythonnet example in their wiki page:
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.