C#調(diào)用python腳本的方法步驟(2種)
因項(xiàng)目需要,需要使用C#控制臺(tái)程序執(zhí)行python腳本,查詢各種資料后可以成功調(diào)用了,記錄一下,以備后面遺忘。
只嘗試了兩種調(diào)用方式,第一種只適用于python腳本中不包含第三方模塊的情況,第二種針對(duì)的是python腳本中包含第三方模塊的情況。不管哪種方式,首先都需要安裝IronPython。我是通過vs2017的工具->NuGet包管理器->管理解決方案的NuGet包,搜索IronPython包安裝,也可以在官網(wǎng)下載安裝包自行安裝后添加引用即可。
方式一:適用于python腳本中不包含第三方模塊的情況
C#代碼
using IronPython.Hosting; using Microsoft.Scripting.Hosting; using System; namespace CSharpCallPython { class Program { static void Main(string[] args) { ScriptEngine pyEngine = Python.CreateEngine();//創(chuàng)建Python解釋器對(duì)象 dynamic py = pyEngine.ExecuteFile(@"test.py");//讀取腳本文件 int[] array = new int[9] { 9, 3, 5, 7, 2, 1, 3, 6, 8 }; string reStr = py.main(array);//調(diào)用腳本文件中對(duì)應(yīng)的函數(shù) Console.WriteLine(reStr); Console.ReadKey(); } } }
python腳本
def main(arr): try: arr = set(arr) arr = sorted(arr) arr = arr[0:] return str(arr) except Exception as err: return str(err)
結(jié)果
方式二:適用于python腳本中包含第三方模塊的情況
C#代碼
using System; using System.Collections; using System.Diagnostics; namespace Test { class Program { static void Main(string[] args) { Process p = new Process(); string path = "reset_ipc.py";//待處理python文件的路徑,本例中放在debug文件夾下 string sArguments = path; ArrayList arrayList = new ArrayList(); arrayList.Add("com4"); arrayList.Add(57600); arrayList.Add("password"); foreach (var param in arrayList)//添加參數(shù) { sArguments += " " + sigstr; } p.StartInfo.FileName = @"D:\Python2\python.exe"; //python2.7的安裝路徑 p.StartInfo.Arguments = sArguments;//python命令的參數(shù) p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.Start();//啟動(dòng)進(jìn)程 Console.WriteLine("執(zhí)行完畢!"); Console.ReadKey(); } } }
python腳本
# -*- coding: UTF-8 -*- import serial import time def resetIPC(com, baudrate, password, timeout=0.5): ser=serial.Serial(com, baudrate, timeout=timeout) flag=True try: ser.close() ser.open() ser.write("\n".encode("utf-8")) time.sleep(1) ser.write("root\n".encode("utf-8")) time.sleep(1) passwordStr="%s\n" % password ser.write(passwordStr.encode("utf-8")) time.sleep(1) ser.write("killall -9 xxx\n".encode("utf-8")) time.sleep(1) ser.write("rm /etc/xxx/xxx_user.*\n".encode("utf-8")) time.sleep(1) ser.write("reboot\n".encode("utf-8")) time.sleep(1) except Exception: flag=False finally: ser.close() return flag resetIPC(sys.argv[1], sys.argv[2], sys.argv[3])
上面的python腳本實(shí)現(xiàn)的是重啟IPC設(shè)備,測(cè)試功能成功。
調(diào)用包含第三方模塊的python腳本時(shí),嘗試過使用path.append()方式,調(diào)試有各種問題,最終放棄了,沒有研究。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C# Winform中實(shí)現(xiàn)主窗口打開登錄窗口關(guān)閉的方法
這篇文章主要介紹了C# Winform中實(shí)現(xiàn)主窗口打開登錄窗口關(guān)閉的方法,這在需要用戶名密碼的軟件項(xiàng)目中是必用的一個(gè)技巧,要的朋友可以參考下2014-08-08CPF?使用C#的Native?AOT?發(fā)布程序的詳細(xì)過程
這篇文章主要介紹了CPF?使用C#的Native?AOT?發(fā)布程序,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具體一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03c#打印預(yù)覽控件中實(shí)現(xiàn)用鼠標(biāo)移動(dòng)頁面功能代碼分享
項(xiàng)目中需要實(shí)現(xiàn)以下功能:打印預(yù)覽控件中,可以用鼠標(biāo)拖動(dòng)頁面,以查看超出顯示范圍之外的部分內(nèi)容,下面就是實(shí)現(xiàn)代碼2013-12-12