因爲項目的須要,須要經過C#調用Python文件(算法)。以前接觸很少,在測試試驗的過程遇到了挺多坑的,在這裏將試驗的過程和結果在這裏總結一下。html
一.使用IronPython做爲移植的依賴庫,直接調用python文件的函數接口。python
百度詞條:IronPython 是一種在 NET 和 Mono 上實現的 Python 語言,由 Jim Hugunin(同時也是 Jython 創造者)所創造。它的誕生是爲了將更多的動態語音移植到NET Framework上。c++
經過簡單的C#代碼實現對python文件內部函數的調用。這種方法能夠說是最簡單直接的,可是,問題來了。IronPython的發佈截止到2011年3月,就是說後面就沒有進行開發和更新了。Python的設計應用突飛猛進,相關的庫的設計和開發層出不窮。不能支持不少第三方庫的引用和加載是這種方法最大的缺陷,簡單的代碼咱們也不必專門寫成python文件去給C#調用,C#本身均可以寫。算法
這種方法建議大夥就別嘗試了,浪費時間。c#
PS:(更正,IronPython仍是有繼續更新發布的,最新版本是 2.7.10,發佈於April 27, 2020。能夠到網頁進行了解https://ironpython.net/,謝謝三樓糾正)異步
二.使用c++程序調用python文件,而後將其作成動態連接庫(dll),在c#中調用此dll文件。函數
這種方法是最開始使用的,但因爲運行速度太慢,因此排除使用,因此後續纔有第三種方法。詳細如何操做,這裏沒研究過就不介紹了(別人開發接口的dll,直接使用沒去了解具體操做)。測試
三.須要安裝python安裝包和庫環境,利用c#命令行,調用.py文件執行(最終使用方法)this
這種方法:經過C#命令行調用.py文件 == 經過python.exe 打開.py文件spa
他的適用性強,你只要保證你的.py程序可以經過python.exe打開,使用就不會有大問題,同時還有一些須要注意的點。
(1)文件路徑不能使用相對路徑(例:path = ./文件名 或者path = 文件名 ),會報錯,這一塊我不清楚是否別人沒遇到,反正個人話是一直會報這種錯誤。
解決方法也很簡單,要麼用絕對路徑,要麼導入os庫,經過os.path.dirname(__file__)能夠獲得當前文件的路徑,即path = os.path.dirname(__file__) + '\文件名'
(2)路徑間隔須要用/代替\;同時「\\」做爲輸入參數偶爾也會有出現異常的狀況,緣由不明。我的建議將輸入路徑參數所有提早替換
(3)不能調用py文件的接口,函數方法
(4)最好在程序前附加異常檢測處理(try,exception),便於獲取異常(C#調用Python偶爾庫,或者一些路徑會有異常,致使直接運行失敗)
if __name__=='__main__': try: #代碼行 a = 1 except Exception as err: #捕捉異常 str1 = 'default:' + str(err) else: # 代碼運行正常 str1 = "Code are operating normally." print(str1)
測試步驟以下:
一、下載安裝python,安裝環境庫
我是經過Notepad++進行python程序編寫,安裝庫直接使用python自帶pip進行安裝。經過CMD直接進行安裝
(1)下面介紹幾個經常使用的pip操做和如何安裝庫
顯示pip的安裝列表:pip list
安裝庫:pip install 庫名
安裝對應版本的庫:pip install --upgrade 庫名==版本號
卸載庫:pip uninstall 庫名
(2)等待安裝完畢便可
(3)注意網速問題,若是網速很差建議直接下載對應庫的離線安裝包(注意)
二、經過VS編寫一個簡單的window窗口進行測試,C#界面和代碼以下
有一些須要注意的地方,首先:文件路徑不能存在空格;輸入參數是路徑的建議所有替換'\\'爲'/';
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Windows.Forms; namespace ToolApp { public partial class Example : Form { private Process progressTest; public Example() { InitializeComponent(); } private void buttonAdd_Click(object sender, EventArgs e) { try { string path = Application.StartupPath + @"\Add.py";//py文件路徑 int a = Convert.ToInt32(this.textBox1.Text); int b = Convert.ToInt32(this.textBox2.Text); StartTest(path, a, b); } catch(Exception e1) { MessageBox.Show(e1.Message); } } /// <summary> /// 開始測試 /// </summary> /// <param name="pathAlg">py文件路徑</param> /// <param name="a">加數a</param> /// <param name="b">加數b</param> /// <returns></returns> public bool StartTest(string pathAlg, int a, int b) { bool state = true; if (!File.Exists(pathAlg)) { throw new Exception("The file was not found."); return false; } string sArguments = pathAlg; sArguments += " " + a.ToString() + " " + b.ToString() + " -u";//Python文件的路徑用「/」劃分比較常見 ProcessStartInfo start = new ProcessStartInfo(); start.FileName = @"python.exe";//環境路徑須要配置好 start.Arguments = sArguments; start.UseShellExecute = false; start.RedirectStandardOutput = true; start.RedirectStandardInput = true; start.RedirectStandardError = true; start.CreateNoWindow = true; using (progressTest = Process.Start(start)) { // 異步獲取命令行內容 progressTest.BeginOutputReadLine(); // 爲異步獲取訂閱事件 progressTest.OutputDataReceived += new DataReceivedEventHandler(outputDataReceived); } return state; } public void outputDataReceived(object sender, DataReceivedEventArgs e) { if (!string.IsNullOrEmpty(e.Data)) { this.Invoke(new Action(() => { this.textBox3.Text = e.Data; })); } } } }
三、經過Notepad++編寫一個簡單的加法函數,調用Python文件代碼以下,導入欄導入一些比較第三方的庫(爲了測試看看第三方庫的導入是否正常)
import numpy import os import sys def Add(a,b): return a+b if __name__=='__main__': try: #代碼行 a = int(sys.argv[1]) b = int(sys.argv[2]) c = Add(a,b) except Exception as err: #捕捉異常 str1 = 'default:' + str(err) else: # 代碼運行正常 str1 = c print(str1)
四、生成成功,經過界面輸入兩組數字,點擊測試,能夠成功獲得結果,效果以下
———————————————————————————————————————————
如何使用IronPython,參考連接:https://www.cnblogs.com/ligiggy/p/11471071.html
四種方法調用,參考連接:https://blog.csdn.net/qq_42063091/article/details/82418630