【问题标题】:Execute python code from C# with process class使用进程类从 C# 执行 python 代码
【发布时间】:2023-03-14 15:05:01
【问题描述】:
ProcessStartInfo start = new ProcessStartInfo();
        start.FileName = ;
        start.Arguments = ;  
        start.UseShellExecute = false;
        start.RedirectStandardOutput = true;
        using (Process process = Process.Start(start))
        {
            using (StreamReader reader = process.StandardOutput)
            {
                string result = reader.ReadToEnd();
                Console.Write(result);
            }
        }

我有这个 python 代码可以从 c# 执行 python。但我不知道在这两个变量中放入什么。

编辑

ProcessStartInfo start = new ProcessStartInfo();
        start.FileName = @"U:\Documents\entsoe-py-master\tests\test_data\python.exe";
        start.Arguments = @"U:\Documents\entsoe-py-master\tests\test_data\request.py 31.12.2016 01.01.2017 datatype";  // give filename, dates from the UI to python and query datatype
        start.UseShellExecute = false;
        start.RedirectStandardOutput = true;    
        using (Process process = Process.Start(start))
        {
            using (StreamReader reader = process.StandardOutput)
            {
                string result = reader.ReadToEnd();
                Console.Write(result);
            }
        }

我收到此错误:

System.ComponentModel.Win32Exception: 'The system cannot find the file specified'

在这条线上using (Process process = Process.Start(start))

【问题讨论】:

  • 是否有可能因为管理员权限而找不到文件?我的意思是,路径很好,这不应该是问题
  • 好的,如果我将文件放在我的 C: 目录中,我会找到该文件。我不知道为什么它在我的 U: 中找不到文件。
  • 我不知道你的U盘在哪里,但是我们在C盘安装python并且进程从C:驱动它自己运行。如果我的回答有帮助,那就采纳吧
  • 这个错误与你在这里问的无关,这个错误与python有关,请再发一个问题;我会要求您在发布问题之前进行研究。 我希望与 Process.Start 或在 C# 中运行 python 文件相关的问题得到解决

标签: c# python process


【解决方案1】:

如果您不使用 shell,则必须将 python 可执行文件的完整路径作为 FileName 提供,并构建 Arguments 字符串以提供您的脚本和要读取的文件。

 ProcessStartInfo start = new ProcessStartInfo();
 start.FileName = cmd;//cmd is full path to python.exe
 start.Arguments = args;//args is path to .py file and any cmd line args
 start.UseShellExecute = false;
 start.RedirectStandardOutput = true;
 using(Process process = Process.Start(start))
 {
     using(StreamReader reader = process.StandardOutput)
     {
         string result = reader.ReadToEnd();
         Console.Write(result);
     }
 }

希望对你有帮助!

【讨论】:

  • 什么是python可执行文件?我在哪里可以找到它?以及脚本和文件有什么区别?
【解决方案2】:

要在c#中运行python代码,需要将python.exe的完整路径作为参数传递给FileName.py文件

ProcessStartInfo start = new ProcessStartInfo();
start.FileName ="Path of python.exe" ;
start.Arguments = "file name ends with .py extension";
start.Verb = "runas";  //To run your process in elevated mode
...remaining code 

【讨论】:

  • 这是我试过的,但它没有找到文件。我有这个错误: System.ComponentModel.Win32Exception: '系统找不到指定的文件'
  • 找不到文件,这意味着你传递了一些错误的 .py 文件路径。在将文件路径作为参数传递之前,您可以使用 File.Exists 方法检查其存在性
  • 请用您目前尝试过的内容以及遇到的错误更新您的问题
  • 我使用了 File.exists 并且 python.exe 和 .py 文件都没有找到。
  • 这意味着您传递了错误的文件路径。如果您是 windows 用户,python exe 的默认路径在 C 驱动器中,即 C:\python3.1\python.exe 不在 Documents 文件夹中,然后转到您的文件想要执行 .py 文件属性并复制其路径
猜你喜欢
  • 2017-03-01
  • 2017-10-20
  • 2021-12-27
  • 2013-08-30
  • 1970-01-01
  • 2018-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多