【发布时间】:2018-06-04 17:22:54
【问题描述】:
我试图从 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))
【问题讨论】:
-
你要么需要当前工作目录中的python文件,要么在你的路径变量中,或者有一个来自当前工作目录的相对路径,或者只是一个绝对路径。
-
你的文件在哪里?现在它正在搜索文件
C:\Users\protieax\PycharmProjects\untitled\venv\Scripts\request.py -
首先,“C#找不到python文件”是什么意思?你有错误吗?如果是这样,请在此处发布错误。
-
@Franck 这几乎肯定不是当前工作目录。它只是 python 可执行文件的路径。