简体   繁体   English

使用进程从C#调用script.py,但获得错误导入模块,例如cv2

[英]Calling script.py from c# with process but getting error import modules like cv2

I'm writing some python code include open-cv. 我正在写一些包含open-cv的python代码。 And now ı have to add some UI and ım using c# but when ı try to call.py 现在,我不得不使用c#添加一些UI和ım,但是当我尝试调用call.py时

ı tried to use the process to call my .py file ı尝试使用该过程来调用我的.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 file must work fine and it must write the results to a .txt file. Python文件必须运行良好,并且必须将结果写入.txt文件。

here is the error: 这是错误:

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

If you added python to path, you can instead of calling process on python, call it on cmd (assuming you are running on Windows), as you would run python script from cmd: "python .../myscript.py args" 如果您将python添加到路径,则可以像在cmd上运行python脚本一样,而不是在python上调用进程,而在cmd上调用它(就像在cmd上运行一样):“ python ... / myscript.py args”

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)){...}

Or you could try using Microsoft.Hosting.Scripting with IronPython. 或者,您可以尝试通过IronPython使用Microsoft.Hosting.Scripting。 :) good luck :) 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM