简体   繁体   English

Python和.NET集成

[英]Python and .NET integration

I'm currently looking at python because I really like the text parsing capabilities and the nltk library, but traditionally I am a .Net/C# programmer. 我目前正在研究python,因为我非常喜欢文本解析功能和nltk库,但传统上我是.Net / C#程序员。 I don't think IronPython is an integration point for me because I am using NLTK and presumably would need a port of that library to the CLR. 我不认为IronPython对我来说是一个集成点,因为我正在使用NLTK,并且可能需要将该库的端口连接到CLR。 I've looked a little at Python for .NET and was wondering if this was a good place to start. 我看了一下Python for .NET ,并想知道这是否是一个好的起点。 Is there a way to marshal a python class into C#? 有没有办法将python类编组成C#? Also, is this solution still being used? 此外,这个解决方案仍在使用吗? Better yet, has anyone done this? 更好的是,有没有人这样做过? One thing I am considering is just using a persistence medium as a go-between (parse in Python, store in MongoDB, and run site in .NET). 我正在考虑的一件事就是使用持久性介质作为中间人(在Python中解析,在MongoDB中存储,在.NET中运行站点)。

NLTK is pure-python and thus can be made to run on IronPython easily. NLTK是纯python,因此可以很容易地在IronPython上运行。 A search turned up this ticket - all one has to do is install a couple of extra Python libraries that don't come by default with IronPython. 搜索出现了这张票 - 所有人必须做的就是安装一些额外的Python库,默认情况下不会使用IronPython。

This is probably the easiest way for you to integrate. 这可能是您集成的最简单方法。 Otherwise, you'll have to either run Python as a subprocess, which sounds complex, or run Python as a server that answers your requests. 否则,您必须将Python作为子进程运行,这听起来很复杂,或者将Python作为响应请求的服务器运行。 This is probably the most scalable, though complex, approach. 这可能是最具扩展性但复杂的方法。 If you go this way, consider Twisted to simplify the server code. 如果你这样做,请考虑使用Twisted来简化服务器代码。

But do try IronPython first... 但是先试试IronPython ......

I don't know why you have a problem with IronPython. 我不知道为什么你有IronPython的问题。 you can still use any and all nltk calls there. 你仍然可以使用任何和所有nltk调用。

To answer your question about porting a Python class into C#: try compiling your python code into an EXE . 回答关于将Python类移植到C#中的问题:尝试将python代码编译为EXE This creates a DLL with all your python classes in it. 这将创建一个包含所有python类的DLL。 This is something that has been around for a while and it has worked like a charm for me in the past 这已经存在了一段时间,它在过去对我来说就像一个魅力

Just an Idea 只是一个想法

How about running Python behind as a server, and connect it from .NET with socket? 如何将Python作为服务器运行,并将其与.NET连接?

Since NLTK loading take time and better load it in advance anyway. 由于NLTK加载需要花费时间并且无论如何都要提前加载它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM