0

The python script is:

import time

for i in range(200):
   print("hey")

How can I run such a simple script from my C# console application?

I'm not trying to covert it over into C#. I want to actually run this python script from my C# program application.

3
  • Why would you want to do that ? for(var i=0; i<200; i++) Console.WriteLine("Hey"); Commented Oct 11, 2020 at 0:33
  • Yeah but the thing is, im not trying to covert it over into C#. I want to actually run this the .py version from my C# program application Commented Oct 11, 2020 at 1:04
  • You would run it like you would any other external program, which in this case would be the path locally installed Python interpreter being passed an argument that's the path to Python script. Commented Oct 11, 2020 at 1:29

2 Answers 2

1

You can try this:

for(var i=0; i<200; i++) Console.WriteLine("Hey");

Credit @Micheal Randall

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah but the thing is, im not trying to covert it over into C#. I want to actually run this the .py version from my C# program application
1

I will System.Diagnostics.Process for this:

using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;

namespace MySpace {
  public class MyProgram {
    public static void Main(string[] args){
    Process p = new Process();
    p.StartInfo = new ProcessStartInfo(@"C:\Python27\python.exe", @"FILEPATH")
    {
        RedirectStandardOutput = true,
        UseShellExecute = false,
        CreateNoWindow = true
    };
    p.Start();
    string output = p.StandardOutput.ReadToEnd();
    p.WaitForExit();
    Console.WriteLine(output);
}}}

This will print the result of the python program to c# console

3 Comments

I really appreciate this answer. I will test it out now. Thanks brother
Hey. This solution worked for my question. However, when I changed my code to this, import pyautogui for i in range(200): print("hey") pyautogui.scroll(-132) The "hey" doesn't display 200 times like before. Do you know what issue does this pertain to?
You are scrolling backwards? (I am not so efficient in PyAutoGUI)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.