打电话脚本.py从c#带进程但得到错误的导入模块,如cv2

2024-10-01 19:20:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写一些python代码,包括opencv。现在必须使用c添加一些UI和m,但是呼叫.py你知道吗

ı尝试使用进程调用我的.py文件

string python = @"C:\Windows.old\Users\giris\AppData\Local\Programs\Python\Python37-32\python.exe";

            string myPythonApp = "C://Users//giris//Desktop//staj_proje_son//main.py";

            string ogrencioptik = textBox2.Text;
            string cevapkagidi = textBox1.Text;
            ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(python);

            myProcessStartInfo.UseShellExecute = false;
            myProcessStartInfo.RedirectStandardOutput = true;
            myProcessStartInfo.Arguments = myPythonApp + " " + ogrencioptik + " " + cevapkagidi;
            Process myProcess = new Process();
            myProcess.StartInfo = myProcessStartInfo;

            myProcess.Start();
            StreamReader myStreamReader = myProcess.StandardOutput;
            string myString = myStreamReader.ReadLine();
            myProcess.WaitForExit();
            myProcess.Close();

Python文件必须工作正常,并且必须将结果写入.txt文件。你知道吗

错误如下:

File "C://Users//giris//Desktop//staj_proje_son//main.py", line 1, in <module>
    import cv2
ModuleNotFoundError: No module named 'cv2'

Tags: 文件textpystringmainusersdesktopson
1条回答
网友
1楼 · 发布于 2024-10-01 19:20:55

如果您将python添加到path,您可以在cmd上调用它(假设您在Windows上运行),而不是在python上调用process,就像在cmd上运行python脚本:“python/myscript.py文件“参数”

string cmdArguments = "/c \"python " + myApp+ " " + sScriptARGS + "\"";

        ProcessStartInfo start = new ProcessStartInfo();
        start.FileName = "cmd.exe";
        start.Arguments = cmdArguments;
        start.RedirectStandardOutput = true;// Any output, generated by application will be redirected back
        start.RedirectStandardError = true; // Any error in standard output will be redirected back (for example exceptions)
        using(Process process = Process.Start(start)){...}

或者你可以试试Microsoft.Hosting.Scripting脚本用铁蟒。:)祝你好运

相关问题 更多 >

    热门问题