Skip to content
Advertisement

Run python script from C# via command prompt

I’m developing a Winform application. I want to run python script from c# via command prompt (i’m using System.Diagnostics.Process class) I have a function to run python script that need to pass a python script file name.

JavaScript

It works correctly until I print a string that contain unicode character. My test.py code contains just a single line.

JavaScript

When I compiled this test.py command prompt from start it worked perfectly. But when I compiled from c# via command prompt I got this error:

JavaScript

I think the problem here caused by C# when it run command prompt and read the python script, the process can not decode utf8 correctly. Thank you.

Advertisement

Answer

After hours finding the reason, I found it. Python decoding doesn’t have problem, that is command prompt problem. So that I fix my function to:

JavaScript

I added sw.WriteLine("chcp 65001"); and sw.WriteLine("set PYTHONIOENCODING=utf-8"); to get better decode unicode. Thank for your attention

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement