1

I'm calling a python script from a C# tool. I based my code on this post. I use ProcessStartInfo to start python and pass a .py file and some argument to it. The code runs fine when the .py, CreateAssessorMap.py, file is on the c drive but not when it is on a mapped network drive. No error is thrown but no python code is executed as far as I can see. If I manually do the same operation from the command line it runs fine.

The code below the first procStartInfo.Arguments will fail as CreateAssessorMap.py is on a network drive. The commented out line below it would work as the script is on the C drive.

    private void btnPrint_Click(object sender, EventArgs e)
    {
        try
        {
            ProcessStartInfo procStartInfo = new ProcessStartInfo();
            procStartInfo.FileName = "python"; 
            procStartInfo.Arguments = string.Format("{0} {1} {2}", @"D:\Map_Generate_Scripts\CreateAssessorMap.py", this.txtSheet.Text, txtDestinationFolder.Text);
            //procStartInfo.Arguments = string.Format("{0} {1} {2} ",  @"C:\Projects\Map_Generate_Scripts\CreateAssessorMap.py", this.txtSheet.Text, txtDestinationFolder.Text);
            procStartInfo.UserName = null;
            procStartInfo.UseShellExecute = false;
            procStartInfo.RedirectStandardOutput = true;

            // Do not create the black window.
            procStartInfo.CreateNoWindow = true;

            // Now you create a process, assign its ProcessStartInfo, and start it.
            using (Process process = Process.Start(procStartInfo))
            {
                using (System.IO.StreamReader reader = process.StandardOutput)
                {
                    string result = reader.ReadToEnd();
                    Console.Write(result);
                }
            }
        }
        catch (Exception ecpt)
        {
            Console.WriteLine(ecpt.Message);
        }
        this.Parent.Hide();
    }

Edit I added some error handling and the python code is failing with the message that the .py file cannot be found.

python: can't open file 'D:\Map_Generate_Scripts\CreateAssessorMapCreateAssessorMap.py': [Errno 2] No such file or directory

I know it exists since I can run the file from the command line with the same arguments. So it seems that when I run from C# the python process can't find the d drive.

Dowlers
  • 1,434
  • 16
  • 26
  • @DavidLee I don't think so. I can call a script from c#, just not when the script is on a network drive which as far as I can tell that question doesn't answer. – Dowlers Sep 07 '17 at 22:14
  • Ahh its on the network, did you try using the actual network path instead of the mapped drive letter? – David Lee Sep 07 '17 at 22:17
  • @DavidLee yes good idea. it works if I use the full path. – Dowlers Sep 07 '17 at 22:51
  • Yes it works if I use the full path. Thank you. But I still need to figure out how to use mapped network drives. The network folks are always moving servers around. – Dowlers Sep 07 '17 at 23:01

2 Answers2

0

I assume that when it runs from a network drive it takes longer and your program does not wait for Python script's completion. I suggest adding proces.Wait() before reading the output stream.

Boris Modylevsky
  • 3,029
  • 1
  • 26
  • 42
  • Thanks for the suggestion but the python script produces output files. The files are not created when run from the network. – Dowlers Sep 07 '17 at 22:24
0

As stated in the comments, the solution for Dowlers was to use the full path instead of the mapped network drive.

Change

@"D:\Map_Generate_Scripts\CreateAssessorMap.py"

To

@"\\[network path]\Map_Generate_Scripts\CreateAssessorMap.py"
David Lee
  • 2,040
  • 17
  • 36