简体   繁体   English

C#代码无法在Visual Studio中运行完整的python脚本

[英]C# code do not run complete python script in visual studio

First of all I do not know if it is a bad practice to call python script from c# so if this is the case please tell me.My current problem is as follows. 首先我不知道从c#调用python脚本是否是一个坏习惯,所以如果是这种情况,请告诉我。我当前的问题如下。

MY c# code only runs the python script partially.... 我的C#代码仅部分运行python脚本。

means (python script create only 4 files when it is supposed to create 10 files) 意思是(python脚本应该创建10个文件时只能创建4个文件)

But When I run my script from cmd in windows I see complete functionality.... 但是,当我在Windows中从cmd运行脚本时,我看到了完整的功能...。

Another thing I saw is when I stop my Visual Studio(2013) I see the complete functionality 我看到的另一件事是,当我停止Visual Studio(2013)时,看到了完整的功能

I am calling the python script(main.py) from c# like this... 我正在像这样从C#调用python脚本(main.py)...

   public JsonResult FetchscrapyDataUrl(String website)
        {

           ProcessStartInfo start = new ProcessStartInfo();
            start.FileName = @"C:\ProgramData\Anaconda3\python.exe";            
            start.Arguments = @"C:\Users\PycharmProjects\scraping_web\scrape_info\main.py";
           //this is path to .py file from scrapy project

            start.CreateNoWindow = false;  // We don't need new window
            start.UseShellExecute = false;  // Do not use OS shell
            //start.RedirectStandardOutput = true;// Any output, generated by application will be redirected back
            start.RedirectStandardError = true; // Any error in standard output will be redirected back (for example exceptions)
            Console.WriteLine("Python Starting");


            start.RedirectStandardOutput = true;
            using (Process process = Process.Start(start))
            {
                using (StreamReader reader = process.StandardOutput)
                {
                    string stderr = process.StandardError.ReadToEnd(); // Here are the exceptions from our Python script
                    string result = reader.ReadToEnd();  // Here is the result of StdOut(for example: print "test")
                    Console.Write(result);
                }
            }

    }

Why I am getting complete script functionality when I stop in Visual Studio(2013)?? 为什么我在Visual Studio(2013)中停止时会获得完整的脚本功能?

I dont understand you motivation behind this. 我不了解您的动机。 But why dont you use IronPython which is excellent addition to the .NET Framework, providing Python developers with the power of the .NET framework. 但是,为什么不使用IronPython,它是.NET Framework的出色补充,为Python开发人员提供了.NET Framework的强大功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM