鲁权屯网站建设,开发一个软件需要什么技术,做网站的方法,企业网站源码 htmlC#调用python函数的常用使用方法有利用Pythonnet、ironPython、打包exe、直接调用、打包成dll等方法 目录
1.IronPython#xff1a;
安装
传参
第三方库
2.Pythonnet
将py文件编译成pyd文件
3.打包exe
3.1 生成exe
3.2 调用exe
3.3 传参
3.3.1 Json与C#之间的转换 …
C#调用python函数的常用使用方法有利用Pythonnet、ironPython、打包exe、直接调用、打包成dll等方法 目录
1.IronPython
安装
传参
第三方库
2.Pythonnet
将py文件编译成pyd文件
3.打包exe
3.1 生成exe
3.2 调用exe
3.3 传参
3.3.1 Json与C#之间的转换
3.3.2 Json与python之间的转换
3.4 传参过程中的问题
问题1No module named XXX
问题2json串中含有引号
问题3json串中含有空格
3.5 加快调用速度
4.直接调用
5.打包成DLL 1.IronPython
ironPython3现在只在github上有源码需要自己编译我尝试失败但是意外发现ironPython2也可以支持py3
安装
ironPython2的安装十分简单安装和简单使用可以参考.NET C# 调用python代码 简单实例_二等碗-CSDN博客_.net 调用python
也可以直接去官网下载exe引导式安装相当方便
传参
调用的代码参考
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Scripting.Hosting;
using IronPython.Hosting;namespace WindowsFormsApp8
{public partial class Form1 : Form{public Form1() InitializeComponent();private void button1_Click(object sender, EventArgs e){if (textBox1.Textstring.Empty){textBox1.Text 0;}if (textBox2.Text string.Empty){textBox2.Text 0;}int num1 Convert.ToInt32(textBox1.Text);int num2 Convert.ToInt32(textBox2.Text);//调用pythonScriptRuntime pyRunTime Python.CreateRuntime();dynamic obj pyRunTime.UseFile(C:/Users/Lee/Desktop/States/test.py);label_result.Text Convert.ToString(obj.add(num1,num2));}private void button2_Click(object sender, EventArgs e){textBox1.Text 0;textBox2.Text 0;label_result.Text ?;}}
}python
import sysdef hello():return hello C#def add(a, b):return (a b)
但是ironPython配置简单但是对py3支持的不是很好对三方库的使用需要额外配置并且不支持pyd文件
第三方库
具体如何配置第三方库可以参考c#利用IronPython调用python的过程种种问题 - monkeyfx - 博客园
C#调用Python脚本并使用Python的第三方模块_dfskchfk72506的博客-CSDN博客
但是连接中使用的egg文件在相应位置是找不到的如果你使用了pip或者conda安装的第三方库因此你需要卸载该三方库并通过源码安装
可以参考python安装第三方库时生成.egg文件_曲終~的博客-CSDN博客 2.Pythonnet
参考C#调用pyd - 灰信网软件开发博客聚合
要使用nuget模块要求VS版本高一点之前使用的VS2010不含此功能2019可以。
将py文件编译成pyd文件
参考python代码加密打包pyd文件在C#中调用_星空的博客-CSDN博客_pyd文件
1.1安装Cython
pip install cython
1.2写好自己要编译的py函数文件test.py
def LOVE(a,b):return ab
1.3然后新建setup.py
from distutils.core import setupfrom Cython.Build import cythonizesetup(ext_modules cythonize(test.py))#其中test.py是写好的待编译的文件
并在命令行里运行
python setup.py build_ext --inplace
1.4 正常情况下会收到
error: Unable to find vcvarsall.bat
如果没报错跳过这一步。
报错是因为VS缺少vcvarsall.bat文件比较简单的方法是安装VS但是VS版本和python有对应要求我使用的是VS2019python3.9还有一点要注意安装VS时要把下面的选项勾选上 1.5 重启电脑之后重新进行1.3步骤会在py文件的同目录下的build\lib.win-amd64-3.9文件夹中得到对应的pyd文件test.cp39-win_amd64.pyd。
1.6 将test.cp39-win_amd64.pyd重命名与py文件相同test.pyd 3.打包exe
会将python的环境一起打包调用时不需要安装python但是运行速度会相对慢一点。
参考https://blog.csdn.net/weixin_43944387/article/details/89945463?utm_mediumdistribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.baidujsdist_request_id1328760.11392.16172458540053725depth_1-utm_sourcedistribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.baidujs
c#调用python的方法总结_逸梵的博客-CSDN博客_c#调用python这个链接实测可以跑通
3.1 生成exe
参考https://jingyan.baidu.com/article/ed2a5d1f03e60749f6be17d2.html
3.2 调用exe
这是我自己实现的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;namespace WindowsFormsApplication1
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){label1.Text waiting . . .;string num1;if (textBox1.Text.Length ! 0)num1 textBox1.Text;elsenum1 0;string pyexePath .\temp.exe; Process p new Process();p.StartInfo.FileName pyexePath;//需要执行的文件路径p.StartInfo.UseShellExecute false; //必需p.StartInfo.RedirectStandardOutput true;//输出参数设定p.StartInfo.RedirectStandardInput true;//传入参数设定p.StartInfo.CreateNoWindow true;p.StartInfo.Arguments num1;//参数以空格分隔如果某个参数为空可以传入””p.Start();string output p.StandardOutput.ReadToEnd();p.WaitForExit();//关键等待外部程序退出后才能往下执行}//Console.Write(output);//输出textBox1.Text Convert.ToString(output);p.Close();label1.Text finish;}private void label1_Click(object sender, EventArgs e){}private void textBox1_TextChanged(object sender, EventArgs e){}}
}注意exe对应的路径代码中是放在Debug目录下debug模式运行
3.3 传参
C#与python之间传参是通过String格式的参数传递对于数组类型的参数特别不友好为了解决这一问题上面链接给出的一个解决方案是可以在C#中将数组写入文件然后将文件地址传递给python但是无端多了些许IO。传参只能以string的格式进行传递因此可以借助Json进行参数传递。
但是有一点要注意
bash传参不支持引号
这就导致字典换后的Json在exe中转python会报错而且报错的唯一反馈是C#中会获取空值。 3.3.1 Json与C#之间的转换
这需要C#中的插件Newtonsoft.Json可以在NuGet直接安装。
使用 Listdouble list new Listdouble();
list.Add(0.7);
list.Add(0.6);
list.Add(0.3);JsonSerializer serializer new JsonSerializer();
StringWriter sw new StringWriter();
serializer.Serialize(new JsonTextWriter(sw), Storage);
string result sw.GetStringBuilder().ToString();
就可以将list转化为result通过调用exe这一小节中介绍的方法进行传参。 传参过程中可能会遇到多个参数类型不同的问题字典也要求值类型相同
为此可以借助如何创建一个在C#中包含不同类型的字典 - 问答 - 云社区 - 腾讯云介绍的HashTable
完整代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;using System.Text;
using System.IO;
using System.Collections;using Newtonsoft.Json;namespace WindowsFormsApplication1
{public static class StorageExtentions{public static T GetT(this Hashtable table, object key){return (T)table[key];}}public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){int a1 12;string a2 test;double a3 24.1;double[] a4 new double[]{1,2.3,4.4,6.3,5.5,7.5};Hashtable Storage new Hashtable();Storage.Add(age, a1);Storage.Add(name, a2);Storage.Add(bmi, a3);Storage.Add(grade, a4);JsonSerializer serializer new JsonSerializer();StringWriter sw new StringWriter();serializer.Serialize(new JsonTextWriter(sw), Storage);string result sw.GetStringBuilder().ToString();MessageBox.Show(result);}}
}通过这种方式可以将所有参数转化为一个json数据。 3.3.2 Json与python之间的转换
这就比较简单使用
json.loads和json.dumps函数即可不再赘述。
但是还有一个eval函数可以是适当使用。
3.4 传参过程中的问题
传参出问题很难调试这是因为传参失败只会返回一个空值不会有任何提示信息。这种C#调用exe实际上是通过命令行来调用我们可以先通过在命令行中运行exe文件来判断错误出在exe中还是C#代码中运行exe的命令为
exe路径 参数1 参数2 参数3
路径可以是绝对路径或者是相对路径参数就是exe中接收的参数。如test.py代码如下在其生成的exe中的参数有2个
#test.py
import sysdef func(a, b):result a breturn resultif __name__ __main__:print(func(sys.argv[1], sys.argv[2]))运行该exe的命令就是test.exe 1 3 (当前处于test.exe所在目录)
问题1No module named XXX
Traceback (most recent call last):File Json_param.py, line 1, in moduleFile PyInstaller\loader\pyimod03_importers.py, line 531, in exec_moduleFile numpy\__init__.py, line 230, in moduleFile PyInstaller\loader\pyimod03_importers.py, line 531, in exec_moduleFile mkl\__init__.py, line 54, in moduleFile mkl\_mkl_service.pyx, line 27, in init mkl._py_mkl_service
ModuleNotFoundError: No module named six
[8128] Failed to execute script Json_param
解决方案pyinstaller打包python文件后运行出现Failed to execute script xxx.exe错误的解决方式(No module named xxx)_武林大皮虾的博客-CSDN博客
six模块虽然没在代码中使用但是间接使用但是pyinstaller打包时并未指定所以执行时找不到此模块
解决方法生成exe时生成了一个同名spec文件 在文件的hiddenimports中写入‘six’然后用该文件重新生成一次exe文件pyinstaller test.spec。
还有另一种解决方案pyinstaller打包遇到的一些坑_HQ1356466973的博客-CSDN博客
如果这个东西不是程序必须得那么打包的时候加入--hidden-import 库名.xxx.xxx不导入它即可解决pyinstaller -Fw yourfile.py --hidden-import 库名.xxx.xxx
但是我没有验证过。
另外一种简单的方法是直接在要运行的函数中import缺失的模块亲测好用
问题2json串中含有引号
引号在json中不能传递转义也不行现在的解决方法是在传递之前replace替换成指定字符串我用的是 shuangyinhao_Archer传递完成后在替换回来有更好的方法希望不吝赐教。
传参可以同时传多个参数因此更好的办法是将每个参数分别Json编码这样基本不会有引号的问题但是空格问题还是要单独处理。这里Json编码的作用是保持数组等数据格式在Python中直接使用eval函数就可以得到原来的形式。
问题3json串中含有空格
命令行相当不智能区分不同参数的唯一方法是用空格因此在生成json串之前替换成指定字符串或者直接去掉字典和类中的空格去掉影响不大
3.5 加快调用速度
https://blog.csdn.net/u014421797/article/details/103302925?utm_mediumdistribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.baidujsdist_request_id1328769.14893.16173739685530525depth_1-utm_sourcedistribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.baidujs 4.直接调用
参考C# 调用 Python直接调用py代码方式_coco的专栏-CSDN博客
5.打包成DLL
参考手把手教你将Python程序打包为DLL_zmr1994的博客-CSDN博客_python生成dll 文章转载自: http://www.morning.zlqyj.cn.gov.cn.zlqyj.cn http://www.morning.ldsgm.cn.gov.cn.ldsgm.cn http://www.morning.dpplr.cn.gov.cn.dpplr.cn http://www.morning.ggtgl.cn.gov.cn.ggtgl.cn http://www.morning.c7627.cn.gov.cn.c7627.cn http://www.morning.blznh.cn.gov.cn.blznh.cn http://www.morning.wnzgm.cn.gov.cn.wnzgm.cn http://www.morning.rbylq.cn.gov.cn.rbylq.cn http://www.morning.wwznd.cn.gov.cn.wwznd.cn http://www.morning.hxwhyjh.com.gov.cn.hxwhyjh.com http://www.morning.nmrtb.cn.gov.cn.nmrtb.cn http://www.morning.dqzcf.cn.gov.cn.dqzcf.cn http://www.morning.tcpnp.cn.gov.cn.tcpnp.cn http://www.morning.wmdbn.cn.gov.cn.wmdbn.cn http://www.morning.jfmjq.cn.gov.cn.jfmjq.cn http://www.morning.ityi666.cn.gov.cn.ityi666.cn http://www.morning.tlfyb.cn.gov.cn.tlfyb.cn http://www.morning.jkpnm.cn.gov.cn.jkpnm.cn http://www.morning.nwjzc.cn.gov.cn.nwjzc.cn http://www.morning.zdhxm.com.gov.cn.zdhxm.com http://www.morning.lfxcj.cn.gov.cn.lfxcj.cn http://www.morning.rjfr.cn.gov.cn.rjfr.cn http://www.morning.wnnts.cn.gov.cn.wnnts.cn http://www.morning.cbchz.cn.gov.cn.cbchz.cn http://www.morning.mxhcf.cn.gov.cn.mxhcf.cn http://www.morning.hhzdj.cn.gov.cn.hhzdj.cn http://www.morning.lwcqh.cn.gov.cn.lwcqh.cn http://www.morning.mbnhr.cn.gov.cn.mbnhr.cn http://www.morning.jqpyq.cn.gov.cn.jqpyq.cn http://www.morning.dqkrf.cn.gov.cn.dqkrf.cn http://www.morning.brps.cn.gov.cn.brps.cn http://www.morning.ndxmn.cn.gov.cn.ndxmn.cn http://www.morning.jfjpn.cn.gov.cn.jfjpn.cn http://www.morning.kjrlp.cn.gov.cn.kjrlp.cn http://www.morning.cnqdn.cn.gov.cn.cnqdn.cn http://www.morning.qnksk.cn.gov.cn.qnksk.cn http://www.morning.qnbgk.cn.gov.cn.qnbgk.cn http://www.morning.thlr.cn.gov.cn.thlr.cn http://www.morning.trbxt.cn.gov.cn.trbxt.cn http://www.morning.mxxsq.cn.gov.cn.mxxsq.cn http://www.morning.dysgr.cn.gov.cn.dysgr.cn http://www.morning.pgmyn.cn.gov.cn.pgmyn.cn http://www.morning.tqxtx.cn.gov.cn.tqxtx.cn http://www.morning.fsfz.cn.gov.cn.fsfz.cn http://www.morning.0dirty.cn.gov.cn.0dirty.cn http://www.morning.gxhqt.cn.gov.cn.gxhqt.cn http://www.morning.zyndj.cn.gov.cn.zyndj.cn http://www.morning.qnwyf.cn.gov.cn.qnwyf.cn http://www.morning.wschl.cn.gov.cn.wschl.cn http://www.morning.hqqpy.cn.gov.cn.hqqpy.cn http://www.morning.rlpmy.cn.gov.cn.rlpmy.cn http://www.morning.mcndn.cn.gov.cn.mcndn.cn http://www.morning.kgxrq.cn.gov.cn.kgxrq.cn http://www.morning.xdfkrd.cn.gov.cn.xdfkrd.cn http://www.morning.stfdh.cn.gov.cn.stfdh.cn http://www.morning.nsncq.cn.gov.cn.nsncq.cn http://www.morning.lczxm.cn.gov.cn.lczxm.cn http://www.morning.lgtzd.cn.gov.cn.lgtzd.cn http://www.morning.ysckr.cn.gov.cn.ysckr.cn http://www.morning.gblrn.cn.gov.cn.gblrn.cn http://www.morning.cpfbg.cn.gov.cn.cpfbg.cn http://www.morning.ngcsh.cn.gov.cn.ngcsh.cn http://www.morning.znpyw.cn.gov.cn.znpyw.cn http://www.morning.yhwmg.cn.gov.cn.yhwmg.cn http://www.morning.nywrm.cn.gov.cn.nywrm.cn http://www.morning.tcpnp.cn.gov.cn.tcpnp.cn http://www.morning.mwqbp.cn.gov.cn.mwqbp.cn http://www.morning.wnqbf.cn.gov.cn.wnqbf.cn http://www.morning.jjxxm.cn.gov.cn.jjxxm.cn http://www.morning.jiuyungps.com.gov.cn.jiuyungps.com http://www.morning.lmhcy.cn.gov.cn.lmhcy.cn http://www.morning.lkhfm.cn.gov.cn.lkhfm.cn http://www.morning.xnltz.cn.gov.cn.xnltz.cn http://www.morning.qnxzx.cn.gov.cn.qnxzx.cn http://www.morning.rwfj.cn.gov.cn.rwfj.cn http://www.morning.bxsgl.cn.gov.cn.bxsgl.cn http://www.morning.ghslr.cn.gov.cn.ghslr.cn http://www.morning.tsflw.cn.gov.cn.tsflw.cn http://www.morning.yggdq.cn.gov.cn.yggdq.cn http://www.morning.ccffs.cn.gov.cn.ccffs.cn