Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have called python file through c# code in windows console application

C#
namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
           
            ProcessStartInfo start = new ProcessStartInfo();
            start.FileName = @"C:\Program Files(x86)\Microsoft Visual Studio\Shared\Python36_64\python.exe";//python path
          
            start.Arguments = @"E:\flowchart_testing\re_exercise.py";
            start.UseShellExecute = false;
            start.CreateNoWindow = true;
            start.RedirectStandardOutput = true;
            string result = "";
            using (Process process = Process.Start(start))
            {
                result = process.StandardOutput.ReadToEnd();
            }
            Console.WriteLine(result);
            Console.ReadKey();
            
        }
    }
}

the python file re_exercise.py contains
Python
print("hello")


the problem is throwing exception could not find the specified file at the start of the process

What I have tried:

i have tried adding python installer to path environment but couldnot resolve it
Posted
Updated 7-Dec-21 2:35am
v2

If you're getting an error about the process not being able to find the specified file, it means that the path to the executable you're launching isn't correct. Have you tried taking the path in:
C#
@"C:\Program Files(x86)\Microsoft Visual Studio\Shared\Python36_64\python.exe";

And pasting it into a command prompt to see what happens? I'm going to wager that if you do you'll get the same error message about the file not existing, and I'm also going wager it's because you might have a mistake in the path:
C#
@"C:\Program Files(x86)\
                  ^

Should there be a space between Program Files and (x86)?
 
Share this answer
 
Hi, I know this is old but maybe it will help someone. I used the following code and it worked as expected.

internal class Program
{
    static void Main(string[] args)
    {
        ProcessStartInfo start = new ProcessStartInfo();
        start.FileName = @"C:\Python27\python.exe";//python path

        start.Arguments = @"C:\Users\james\OneDrive\Desktop\ver1.py";
        start.UseShellExecute = false;
        start.CreateNoWindow = true;
        start.RedirectStandardOutput = true;
        string result = "";
        using (Process process = Process.Start(start))
        {
            result = process.StandardOutput.ReadToEnd();
        }
        Console.WriteLine(result);
        Console.ReadKey();
    }
}


For the script I kept it really simple:

import sys

print(sys.version)


Saved as ver1.py to my desktop as you can see.

Result -- I got this in the console window:

2.7.16 (v2.7.16:413a49145e, Mar  4 2019, 01:37:19) [MSC v.1500 64 bit (AMD64)]


Good luck, never quit just keep coding!

JR
 
Share this answer
 
Comments
Richard Deeming 5-Apr-24 4:56am    
A code block that is virtually identical to the code in the question, along with the statement that it worked fine for you, is not a "solution". Particularly not to a question from over two years ago!

Chris already explained what the OP's problem was, and how to fix it. You have added nothing new to the discussion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900