Run Python.PY script from C #

I have this Python code.

import pyodbc
import time

print("Hello")
plexString = "{call sproc164407_2053096_651466 ()}"
connectionPlex = pyodbc.connect('DSN=PlexReport32; UID=XXXX; PWD=XXX', autocommit = True)
cursorPlex = connectionPlex.cursor()

connectionLocal = pyodbc.connect("DRIVER={SQL Server}; SERVER=XXX; DATABASE=Plex; Trusted_Connection=yes; connection timeout=30")
cursorLocal = connectionLocal.cursor()

cursorPlex.execute(plexString)
rows = cursorPlex.fetchall()

for row in rows:
    date1 = row[1].rstrip("0")
    date2 = row[2].rstrip("0")
    row[1] = date1
    row[2] = date2
    cursorLocal.execute('INSERT INTO Regraded_Serials VALUES (?,?,?,?,?,?,?,?,?,?,?,?)', row)
    cursorLocal.commit()

      

It is saved as a PlexStoredProcedures.pyw file in the folder. I used pyw because I was reading this to stop it from opening a console window. I am using this python script to fetch data from a remote database and add it to my local SQL server. C # has problems with this that are out of control.

But I cannot just execute the script. I don't need to add any arguments, and I don't need to return anything. I just want it to run the script, and for C #, wait for it to finish before continuing. I've looked online, but the answers to this simple question are always far from clear. This is the best I have in C #. It works, but the script doesn't work, or at least the script doesn't grab and insert the data it does if I run it manually.

C # code:

try
            {
                ProcessStartInfo pythonInfo = new ProcessStartInfo();
                Process python;
                pythonInfo.FileName = @"C:\Visual Studio Projects\PlexStoredProcedures\PlexStoredProcedures\PlexStoredProcedures.pyw";
                //pythonInfo.Arguments = string.Format("{0} {1}", cmd, args);
                pythonInfo.CreateNoWindow = false;
                pythonInfo.UseShellExecute = true;

                Console.WriteLine("Python Starting");
                python = Process.Start(pythonInfo);
                python.WaitForExit();
                python.Close();
                Console.WriteLine("Python Exiting");

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

      

I know this is probably something very basic, but I can't seem to find what I need.

Also, I would like to run this in IronPython, but IronPython doesn't seem to add the pyobbc module.

Any suggestions would be helpful, but would be more helpful if you could show me how to do this?

+2


source to share


1 answer


Have you tried executing python.exe with a script as an argument? It can be as simple as a script to incorrectly execute on its own.

Like this:



ProcessStartInfo pythonInfo = new ProcessStartInfo();
Process python;
pythonInfo.FileName = @"C:\Python27\python.exe";
pythonInfo.Arguments = @"C:\Visual Studio Projects\PlexStoredProcedures\PlexStoredProcedures\PlexStoredProcedures.pyw";
pythonInfo.CreateNoWindow = false;
pythonInfo.UseShellExecute = true;

Console.WriteLine("Python Starting");
python = Process.Start(pythonInfo);
python.WaitForExit();
python.Close();

      

+3


source







All Articles