前言:
在平时工作中,需求有多种实现方式;根据不同的需求可以采用不同的编程语言来实现。发挥各种语言的强项
如:Python的强项是:数据分析、人工智能等
.NET 开发桌面程序界面比Python更简单、方便、美观
那么就存在各种语言间交互,本篇主要介绍几种C# 与 Python的交互方式。
一、IronPython
1、IronPython是什么?
IronPython是Python编程语言的开源实现,该语言与.NET Framework紧密集成。IronPython可以使用.NET Framework和Python库,而其他.NET语言也可以轻松使用Python代码。
2、IrconPython如何使用
a) 添加包引用:
1
|
Install-Package IronPython -Version 2.7.11 |
b) 执行简单python脚本:
1
2
3
4
5
6
7
8
|
//创建执行引擎 ScriptEngine engine = Python.CreateEngine(); ScriptScope scope = engine.CreateScope(); string script = "'Hello %d' %number" ; //设置参数列表 scope.SetVariable( "number" , 123); ScriptSource sourceCode = engine.CreateScriptSourceFromString(script); var result = sourceCode.Execute< object >(scope); |
c) 执行python文件方法:
添加如下简单Python文件:demo.py
1
2
3
4
5
6
7
8
9
10
11
|
#文件目录 demo.py import time def welcome(name): return "hello " + name def Add(a, b): return a + b def showMsg(name,birthday): return "姓名:" + name + ",生日:" + birthday |
执行方法:
1
2
3
4
5
|
string path = @"demo.py" ; ScriptRuntime pyRuntime = Python.CreateRuntime(); //创建一下运行环境 dynamic obj = pyRuntime.UseFile(path); //调用python中Add函数 object sum = obj.Add(1, 54); |
d) 执行python文件方法(依赖三方库):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
from scipy import linalg import numpy as np import json def calcnum(jsonInput): A = np.array([[ 1 , 1 , 7 ], [ 2 , 3 , 5 ], [ 4 , 2 , 6 ]]) # A代表系数矩阵 b = np.array([ 2 , 3 , 4 ]) # b代表常数列 x = linalg.solve(A, b) # json字符串转对象 info = json.loads(jsonInput) print (info[ 'a' ]) # 对象转json字符串 output = json.dumps(info) print (output) print (x) |
执行结果:
IronPython虽然能够交互Python,但支持的库不多,当py文件依赖三方库,则会调用失败;且IronPython对Python3尚未完全支持,下面就开始在C#在Python有第三方库的情况下交互Python。
二、Python打包exe调用
把Python打包成exe,C#再通过cmd进行交互。就可以解决第三方库的问题;
修改Python文件(calc.py):依赖了scipy库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
from scipy import linalg import numpy as np import json def calcnum(jsonInput): A = np.array([[ 1 , 1 , 7 ], [ 2 , 3 , 5 ], [ 4 , 2 , 6 ]]) # A代表系数矩阵 b = np.array([ 2 , 3 , 4 ]) # b代表常数列 x = linalg.solve(A, b) # json字符串转对象 info = json.loads(jsonInput) print (info[ 'a' ]) # 对象转json字符串 output = json.dumps(info) print (output) print (x) if __name__ = = '__main__' : inputString = sys.argv[ 1 ] calcnum(inputString) |
Pyinstaller:
PyInstaller 是一个程序,将(包)Python 程序打包到独立的可执行文件中,在 Windows、Linux、Mac OS X、FreeBSD、Solaris 和 AIX 下。与类似工具不同,它的主要优点是 PyInstaller 可以与 Python 2.7 和 3.3_3.5 配合使用,它通过透明压缩构建更小的可执行文件,它是完全多平台的,并使用操作系统支持来加载动态库,从而确保完整兼容性。
PyInstaller 的主要目标是与开箱即用的第三方软件包兼容。这意味着,使用 PyInstaller 时,所有使外部包正常工作所需的技巧都已集成到 PyInstaller 本身中,因此无需用户干预。您永远不需要在 wiki 中查找技巧,并将自定义修改应用于文件或设置脚本。例如,像PyQt、Django或matplotlib这样的库完全支持,而无需手动处理插件或外部数据文件。
安装:
在Cmd命令行中运行:需提前安装pip(python3.4以后默认包含)
1
|
pip install pyinstaller |
打包:
1
2
|
//命令格式 pyinstaller 选项 Python 源文件 |
选项参数支持以下内容:
参数 | 描述 |
---|---|
-H, -help | 查看帮助文档 |
-F,-onefile | 产生单个的可执行文件 |
-D,--onedir | 产生一个目录(包含多个文件)作为可执行程序 |
-a,--ascii | 不包含 Unicode 字符集支持 |
-d,--debug | 产生 debug 版本的可执行文件 |
-w,--windowed,--noconsolc | 指定程序运行时不显示命令行窗口(仅对 Windows 有效) |
-c,--nowindowed,--console | 指定使用命令行窗口运行程序(仅对 Windows 有效) |
-o DIR,--out=DIR | 指定 spec 文件的生成目录。如果没有指定,则默认使用当前目录来生成 spec 文件 |
-p DIR,--path=DIR | 设置 Python 导入模块的路径(和设置 PYTHONPATH 环境变量的作用相似)。也可使用路径分隔符(Windows 使用分号,Linux 使用冒号)来分隔多个路径 |
-n NAME,--name=NAME | 指定项目(产生的 spec)名字。如果省略该选项,那么第一个脚本的主文件名将作为 spec 的名字 |
最后在命令行中执行命令:
pyinstaller -F calc.py
打包完成后,生成calc.exe文件:
调用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
private string Calc( string inputString) { // 调用python脚本 Process p = new Process(); p.StartInfo.FileName = "calc.exe" ; p.StartInfo.UseShellExecute = false ; p.StartInfo.RedirectStandardOutput = true ; p.StartInfo.RedirectStandardInput = true ; p.StartInfo.CreateNoWindow = true ; // 注意,这前面得有一个空格,不然就连在一起了 p.StartInfo.Arguments = " " + inputString; p.Start(); string output = p.StandardOutput.ReadToEnd(); p.WaitForExit(); p.Close(); return output; } |
到此该方法已能成功调用Python结果
三、Python提供WebApi接口(推荐)
使用Python中flask库实现一个简单的webapi服务:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
from flask import Flask, request import Math app = Flask(__name__) @app .route( "/" ) def welcome(): return "Hello World!" @app .route( "/calc" ) def calc(): strinput = request.args.get( "jsonInput" ) return Math.calcnum(strinput) if __name__ = = "__main__" : app.run() |
最后在C#中调用webapi:得到python结果
总结:
通过以上内容知道各种语言平台想交互,虽然有多种方式实现;但是最为推荐还是提供Webapi接口方式。
以上就是详解c#与python的交互方式的详细内容,更多关于c#与python的交互的资料请关注服务器之家其它相关文章!
原文链接:https://www.cnblogs.com/cwsheng/p/14673491.html