gpt4 book ai didi

c# - 从 C# 执行 python

转载 作者:行者123 更新时间:2023-12-05 07:33:19 25 4
gpt4 key购买 nike

我试图从 C# 执行一些 python 代码,所以我有这段代码:

ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @"C:\Users\protieax\PycharmProjects\untitled\venv\Scripts\python.exe";
start.Arguments = "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);
}
}

它应该可以工作,但 C# 找不到 python 文件。

我应该在这两个变量中输入什么?谢谢

编辑:

ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @"U:\Documents\entsoe-py-master\tests\test_data";
start.Arguments = "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.Stqrt(start))

最佳答案

您指定了一个 U 盘,大概是一个网络驱动器。我从来没有在这样的环境中使用过网络驱动器,但我试过它以查明是否是问题所在。我安装了一个以 U 为标签的网络驱动器。运行代码后,它失败并给了我同样的错误。我建议将所需的 python 脚本复制到您当前的工作目录中,这会使事情变得更加简单。

关于c# - 从 C# 执行 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50685762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com