从 C# 运行 Python .PY 脚本 [英] Run Python .PY script from C#

查看:80
本文介绍了从 C# 运行 Python .PY 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个python代码.

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()

它在文件夹中保存为名为 PlexStoredProcedures.pyw 的文件.我使用 pyw 是因为我读到这会阻止它打开控制台窗口.我使用这个 python 脚本从远程数据库中提取数据并将其添加到本地 sql 服务器.C# 有我无法控制的问题.

It is saved as a file called PlexStoredProcedures.pyw in a folder. I have used pyw because I read this stops it from opening a console window. I use this python script to extract data from a remote database and add it to the local sql server. C# has issues with this that are beyond my control.

但我似乎不能只执行脚本.我不需要添加任何参数,也不需要它返回任何东西.我只希望它运行脚本,并让 C# 在继续之前等待它完成.我在网上看过,但这个简单问题的答案总是很难理解.这是迄今为止我在 C# 中最好的.它正在工作,但它没有运行脚本,或者至少,脚本没有抓取和插入数据,如果我手动运行它会这样做.

But I can't seem to just execute the script. I don't need to add any arguments, and I don't need it to return anything back. I just want it to run the script, and for C# to wait for it to finish before continuing. I have looked online but the answers to this simple question are always far from easy to understand. This is the best I have so far in C#. It is working, but it isn't running the script, or at least, the script is not grabbing and inserting the data, which it does if I run in manually.

C# 代码:

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 it is probably something really simple but I can't seem to find what It is that I need to do.

另外,我很想在 IronPython 中运行它,但显然 IronPython 不会添加模块 pyodbc.

Also, I would love to run this in IronPython but apparently IronPython will not add the module pyodbc.

任何建议都会有所帮助,但如果您能告诉我如何做,也会更有帮助?

Any suggestions would be helpful, but more helpful would be also if you can show me how?

推荐答案

您是否尝试过以脚本为参数执行 python.exe?可能就像脚本本身没有正确执行一样简单.

Have you tried executing python.exe with the script as an argument? It may be as simple as the script not properly executing on its own.

像这样:

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();

这篇关于从 C# 运行 Python .PY 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆