C#中使用python(基于Ironpython)
----IronPython 是一种在 NET 和 Mono 上实现的 Python 语言,由 Jim Hugunin(同时也是 Jython 创造者)所创造.
IronPython是优雅的python编程语言和强大的.NET平台的有机结合。使用动态类型系统得以将Python移植到NET Framework上。
可以形象的说,Ironpython就像是Python的一种方言,所以装了Ironpython就已经是安装了Python了。----Ironpython就是在Python.NET下的实现,CPython是C下的Python实现,都是同一个东东,所以装了Ironpython并不需要装Python,当然两者也可以共存。----不需要,Ironpython就是.NET下实现的Python
只有python2


test.py
def main(arr):try:arr = set(arr)arr = sorted(arr)arr = arr[0:2]return str(arr)except Exception as err:return str(err)
private void button1_Click(object sender, EventArgs e){ScriptEngine pyEngine = Python.CreateEngine();//创建Python解释器对象dynamic py = pyEngine.ExecuteFile(@"test.py");//读取脚本文件string dd = py.main(textBox1.Lines);//调用脚本文件中对应的函数textBox2.Text += dd + "\r\n";}

在IronPython使用第三方库
在.py文件中直接添加,本地库的目录就行了:
import syssys.path.append(r"E:\Anaconda2\Lib")sys.path.append(r"E:\Anaconda2\Lib\site-packages")import ****第三方库
这样太麻烦了,不如这样
1.打包成exe
2.用C#直接调用python的exe
3.参数的传递通过C#创建一个txt文档
4.在python程序中读取这个txt文档
5.最终达到传递参数的效果
注意:如果生成的exe,里面有whlie True等一直等待,打包的时候又没有显示窗口,记得要注销exe