前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#调用Python方式

C#调用Python方式

原创
作者头像
软件工程师Michael
发布2022-10-27 22:25:33
1.4K0
发布2022-10-27 22:25:33
举报

Python是AI领域的最主流的编程语言,没有之一。而应用开发领域则通常不会选用Python语言。如果遇到应用开发过程中涉及AI算法,那就必然要面对跨语言通讯的问题。今天来介绍下C#中执行Python脚本的方式之一,当然还有其他方式也能实现。

需要安装python安装包和库环境,利用c#命令行,调用.py文件执行

  这种方法:通过C#命令行调用.py文件 ==?通过python.exe?打开.py文件

  他的适用性强,你只要保证你的.py程序能够通过python.exe打开,使用就不会有大问题,同时还有一些需要注意的点。

  (1)文件路径不能使用相对路径(例:path = ./文件名 或者path = 文件名 ),会报错,这一块我不清楚是否别人没遇到,反正我的话是一直会报这种错误。

  解决方法也很简单,要么用绝对路径,要么导入os库,通过os.path.dirname(__file__)可以得到当前文件的路径,即path =?os.path.dirname(__file__) + '\文件名'

  (2)路径间隔需要用/代替\;同时“\\”作为输入参数偶尔也会有出现异常的情况,原因不明。个人建议将输入路径参数全部提前替换

  (3)不能调用py文件的接口,函数方法

  (4)最好在程序前附加异常检测处理(try,exception),便于获取异常(C#调用Python偶尔库,或者一些路径会有异常,导致直接运行失败)

准备一个简单的Winform程序和Python脚本。

代码语言:javascript
复制
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.Threading.Tasks;
using System.Windows.Forms;

namespace CsharpCallPython
{
    public partial class Form1 : Form
    {
        private Process progressTest;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                //string path = Application.StartupPath + @"\CsharpCallPython.py";//py文件路径";
                string path= "F:\\study\\mycode\\python\\PythonTest\\flaskdemo\\mypthondemo.py";
                int a = Convert.ToInt32(txtA.Text);
                int b = Convert.ToInt32(txtB.Text);
                StartTest(path, a, b);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }

        public void outputDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.Data))
            {
                this.Invoke(new Action(() => {
                    this.txtResult.Text = e.Data;
                }));
            }
        }

        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 = @"D:\Python36\python.exe";//环境路径需要配置好Python的实际安装路径
            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;
        }

    }
}

Python脚本如下:

代码语言:javascript
复制
# -*- coding: utf-8 -*-
"""
File Name:   PythonTest
Author:      Michael
date:        2022/10/27
"""

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)

运行Winform程序,结果如下:

winform中执行Python脚本
winform中执行Python脚本

显然,结果完成准确。

【小结】

C#直接执行Python脚本,可以行得通。但这并不是跨语言通信的一般处理方式,以后还得深入研究一下RPC框架。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com