Home > Net >  C # to call python suddenly appear problem, StandardOutput
C # to call python suddenly appear problem, StandardOutput

Time:09-21

C # call python suddenly appear problem, I do GNSS before using CMD call python, ready to change the program today, my hand is out of control with boring tinder, clean up the file with the registry, and then not move before success code, run a python output is null, I again make the python environment remains no fixed (py test files can be started from the vs and CMD command to start the output),
Part of the code
Public static void RunPythonScript (string sArgName, string args="", params string [] teps)
{
The Process of p=new Process ();
//string path=System. The AppDomain. CurrentDomain. SetupInformation. ApplicationBase + sArgName;//get the python file's absolute path (put file in c # the debug folder can operation)
String path=@ "D: \ ana \ Lib" + sArgName;//(because I didn't put the debug, so write directly absolute path, replace the path above)
P. tartInfo. FileName=@ "D: \ ana \ python exe";//(note: the words into your own) doesn't match the environment variable, you can like me to write python. Exe the absolute path (words into your own), if match, write directly "python. Exe can
"String sArguments=path;
The foreach (string sigstr in teps)
{
SArguments +="" + sigstr;//pass parameters
}

SArguments +="" + args.

P. tartInfo. The Arguments=sArguments;

P. tartInfo. UseShellExecute=false;

P. tartInfo. RedirectStandardOutput=true;

P. tartInfo. RedirectStandardInput=true;

P. tartInfo. RedirectStandardError=true;

P. tartInfo. CreateNoWindow=false;

P. tart ();
P.B eginOutputReadLine ();

P.O utputDataReceived +=new DataReceivedEventHandler (p_OutputDataReceived);

Console.ReadLine();

P.W aitForExit ();
}
//output print information
The static void p_OutputDataReceived (object sender, DataReceivedEventArgs e)
{
if (! String. IsNullOrEmpty (e.D ata))
{
String b;
Console. WriteLine (e.D ata + Environment. NewLine);


String a=e.D ata.
b=a;





String [] strArr=a.S plit (");
IntArr=new double [strArr Length];



for (int i=0; I & lt; StrArr. Length; I++)
{
IntArr [I]=the Convert. ToDouble (strArr [I]);
}
A=b;
;



}

}
E.d ate value is null, I tried to reinstall the application and no role in doubt CMD failed to lift the python, or StandardOutput gave an issue,

CodePudding user response:

A debug method is by calling the CMD command line, and removing RedirectStandardOutput etc. The value of the attribute, a default value is set false

Then performed by the CMD command to start the Py then let Py command, you can see the Py have to output, and the content of the output, according to the contents of the output can be the next step debugging

How the CMD incoming parameters see [CMD. Exe command-line launch parameters (can be used to execute the command, the cords or environment configuration) - walterlv] (https://walterlv.com/post/cmd-startup-arguments.html)

[how to start CMD software to take the path of the space] (https://blog.lindexi.com/post/%E5%A6%82%E4%BD%95%E5%9C%A8-CMD-%E5%90%AF%E5%8A%A8%E7%9A%84%E8%BD%AF%E4%BB%B6%E4%BC%A0%E5%85%A5%E5%B8%A6%E7%A9%BA%E6%A0%BC%E7%9A%84%E8%B7%AF%E5%BE%84.html)

CodePudding user response:

reference 1st floor lindexi_gd response:
a debug method is by calling the CMD command line, and removing RedirectStandardOutput etc. The value of the attribute, a default value is set false

Then performed by the CMD command to start the Py then let Py command, you can see the Py have to output, and the content of the output, according to the contents of the output can be the next step debugging

How the CMD incoming parameters see [CMD. Exe command-line launch parameters (can be used to execute the command, the cords or environment configuration) - walterlv] (https://walterlv.com/post/cmd-startup-arguments.html)

[how to start CMD software to take the path of the space] (https://blog.lindexi.com/post/%E5%A6%82%E4%BD%95%E5%9C%A8-CMD-%E5%90%AF%E5%8A%A8%E7%9A%84%E8%BD%AF%E4%BB%B6%E4%BC%A0%E5%85%A5%E5%B8%A6%E7%A9%BA%E6%A0%BC%E7%9A%84%E8%B7%AF%E5%BE%84.html)

Before I can run, code didn't change, estimate or environment configuration has a problem, I just use standardoutput executing packed program directly, the console successful real data, don't know why this way out of the question,

CodePudding user response:

refer to the second floor qq_43343885 response:
Quote: refer to 1st floor lindexi_gd response:
a debug method is by calling the CMD command line, and removing RedirectStandardOutput etc. The value of the attribute, a default value is set false

Then performed by the CMD command to start the Py then let Py command, you can see the Py have to output, and the content of the output, according to the contents of the output can be the next step debugging

How the CMD incoming parameters see [CMD. Exe command-line launch parameters (can be used to execute the command, the cords or environment configuration) - walterlv] (https://walterlv.com/post/cmd-startup-arguments.html)

[how to start CMD software to take the path of the space] (https://blog.lindexi.com/post/%E5%A6%82%E4%BD%95%E5%9C%A8-CMD-%E5%90%AF%E5%8A%A8%E7%9A%84%E8%BD%AF%E4%BB%B6%E4%BC%A0%E5%85%A5%E5%B8%A6%E7%A9%BA%E6%A0%BC%E7%9A%84%E8%B7%AF%E5%BE%84.html)

Before I can run, code didn't change, estimate or environment configuration has a problem, I just use standardoutput executing packed program directly, the console successful real data, don't know why this way out of the question,


Indeed py environment problem is more, the worst method, in line with steps once again
  •  Tags:  
  • C#
  • Related