title: How do I run a Python script from C#?
body: <p>This sort of question has been asked before in varying degrees, but I feel it has not been answered in a concise way and so I ask it again.</p>
  
  <p>I want to run a script in Python. Let's say it's this:</p>
  
  <pre><code>if __name__ == '__main__':
      with open(sys.argv[1], 'r') as f:
          s = f.read()
      print s
  </code></pre>
  
  <p>Which gets a file location, reads it, then prints its contents. Not so complicated.</p>
  
  <p>Okay, so how do I run this in C#?</p>
  
  <p>This is what I have now:</p>
  
  <pre><code>    private void run_cmd(string cmd, string args)
      {
          ProcessStartInfo start = new ProcessStartInfo();
          start.FileName = cmd;
          start.Arguments = args;
          start.UseShellExecute = false;
          start.RedirectStandardOutput = true;
          using (Process process = Process.Start(start))
          {
              using (StreamReader reader = process.StandardOutput)
              {
                  string result = reader.ReadToEnd();
                  Console.Write(result);
              }
          }
      }
  </code></pre>
  
  <p>When I pass the <code>code.py</code> location as <code>cmd</code> and the <code>filename</code> location as <code>args</code> it doesn't work. I was told I should pass <code>python.exe</code> as the <code>cmd</code>, and then <code>code.py filename</code> as the <code>args</code>.</p>
  
  <p>I have been looking for a while now and can only find people suggesting to use IronPython or such. But there must be a way to call a Python script from C#.</p>
  
  <p>Some clarification:</p>
  
  <p>I need to run it from C#, I need to capture the output, and I can't use IronPython or anything else. Whatever hack you have will be fine.</p>
  
  <p>P.S.: The actual Python code I'm running is much more complex than this, and it returns output which I need in C#, and the C# code will be constantly calling the Python code.</p>
  
  <p>Pretend this is my code:</p>
  
  <pre><code>    private void get_vals()
      {
          for (int i = 0; i &lt; 100; i++)
          {
              run_cmd("code.py", i);
          }
      }
  </code></pre>