Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

run python script from c#

using IronPython.Hosting;
using Microsoft.Scripting.Hosting;

private static void doPython()
{
    ScriptEngine engine = Python.CreateEngine();
    engine.ExecuteFile(@"test.py");
}

//You can get IronPython here : https://ironpython.net/
Comment

call python script from c#

private void run_cmd(string cmd, string args)
{
     ProcessStartInfo start = new ProcessStartInfo();
     start.FileName = "my/full/path/to/python.exe";
     start.Arguments = string.Format("{0} {1}", cmd, 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);
         }
     }
}
Comment

call python from c#

using System; 
using System.IO; 
using System.Diagnostics; 

namespace CallPython 
{ 
    /// <summary> 
    /// Used to show simple C# and Python interprocess communication 
    /// Author      : Ozcan ILIKHAN 
    /// Created     : 02/26/2015 
    /// Last Update : 04/30/2015 
    /// </summary> 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            // full path of python interpreter 
            string python = @"C:ContinuumAnacondapython.exe"; 

            // python app to call 
            string myPythonApp = "sum.py"; 

            // dummy parameters to send Python script 
            int x = 2; 
            int y = 5; 

            // Create new process start info 
            ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(python); 

            // make sure we can read the output from stdout 
            myProcessStartInfo.UseShellExecute = false; 
            myProcessStartInfo.RedirectStandardOutput = true; 

            // start python app with 3 arguments  
            // 1st arguments is pointer to itself,  
            // 2nd and 3rd are actual arguments we want to send 
            myProcessStartInfo.Arguments = myPythonApp + " " + x + " " + y; 

            Process myProcess = new Process(); 
            // assign start information to the process 
            myProcess.StartInfo = myProcessStartInfo; 

            Console.WriteLine("Calling Python script with arguments {0} and {1}", x,y); 
            // start the process 
            myProcess.Start(); 

            // Read the standard output of the app we called.  
            // in order to avoid deadlock we will read output first 
            // and then wait for process terminate: 
            StreamReader myStreamReader = myProcess.StandardOutput; 
            string myString = myStreamReader.ReadLine(); 

            /*if you need to read multiple lines, you might use: 
                string myString = myStreamReader.ReadToEnd() */           

            // wait exit signal from the app we called and then close it. 
            myProcess.WaitForExit(); 
            myProcess.Close(); 

            // write the output we got from python app 
            Console.WriteLine("Value received from script: " + myString); 

        } 
    } 
} 
Comment

run python from c#

string strCmdText;
string file;
file = "py.py"
strCmdText= "python3" + file;
System.Diagnostics.Process.Start("CMD.exe",strCmdText);
Comment

PREVIOUS NEXT
Code Example
Python :: how to inherit a class in python 
Python :: python object creation 
Python :: python path absolute 
Python :: get length from variable python 
Python :: if we use list in the dictionary 
Python :: set empty dictionary key python 
Python :: how to work with django ornm __in 
Python :: tuple unpacking 
Python :: avoid self python by making class functions static 
Python :: python os.walk 
Python :: quiz game in python 
Python :: docstring python 
Python :: new line 
Python :: what is a thread in os 
Python :: pandas transform vs filter 
Python :: rotatelist in python 
Python :: pynput keyboard backspace 
Python :: selenium python get image from url 
Python :: odd number in python 
Python :: python type checking boolean 
Python :: how to use underscore in python 
Python :: pytube3 
Python :: python how to make a user input function 
Python :: python list to sublists 
Python :: get status code python 
Python :: data.head on terminal 
Python :: remove percentage in python 
Python :: planet earth minecraft 
Python :: In is_lodes_form( : Missing id-axis pairings. 
Python :: max(X_train, key=len).split() 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =