c# 调用python

懵懂的女人 提交于 2020-08-07 08:39:23
using IronPython.Compiler.Ast;
using IronPython.Hosting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace WpfAppforpython
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var psi = new ProcessStartInfo();
            psi.FileName = @"C:\ProgramData\Anaconda3\python.exe";

            var script = @"C:\Users\Administrator\app.py";

            psi.Arguments = $"\"{script}\"";

            psi.UseShellExecute = false;
            psi.CreateNoWindow = true;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardError = true;

            var errors = "";
            var results = "";

            using (var process = Process.Start(psi))
            {
                errors = process.StandardError.ReadToEnd();
                results = process.StandardOutput.ReadToEnd();
            }



            Console.WriteLine("Error");
            Console.WriteLine(errors);
            Console.WriteLine();
            Console.WriteLine("Results");
            Console.WriteLine(results);

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            //1 
            var engine = Python.CreateEngine();

            //2
            var script = @"C:\Users\Administrator\app.py";
            var source = engine.CreateScriptSourceFromFile(script);

            //var argv = new List<string>();
            //argv.Add("");

            //engine.GetSysModule().SetVariable("argv", argv);

            //3
            var eIO = engine.Runtime.IO;

            var errors = new MemoryStream();
            eIO.SetErrorOutput(errors, Encoding.Default);

            var results = new MemoryStream();
            eIO.SetOutput(results, Encoding.Default);

            //4 
            var scope = engine.CreateScope();
            source.Execute(scope);

            //5
            string str(byte[] x) => Encoding.Default.GetString(x);

            Console.WriteLine("Error");
            Console.WriteLine(str(errors.ToArray()));
            Console.WriteLine();
            Console.WriteLine("Results");
            Console.WriteLine(str(results.ToArray()));
        }
    }
}

调用了anaconda的python3.8 exe, 直接运行python脚本。

问题是打包没有实现。

pyinstaller对第三方包,特别是matpoltlib难以实现;nuitka7个小时都没能完成打包。

ironpython对第三方包几乎都无法支持,还是matplotlib。

现在的情况是如果直接使用C#进行科学计算和绘图,则方便打包。

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!