1

I'm running arcPy commands from a C# script i.e.

RunPythonScript("arcpy.Delete_management(\"C:\\\\Temp\\\\NW\\\\land.gdb\", \"Table\")");

private static void RunPythonScript(string script)
        {
            try
            {
                Stopwatch stpw = new Stopwatch();
                stpw.Start();

                string pyfile = "C:\\Temp\\cmd.py";

                StreamWriter sw = new StreamWriter(pyfile);
                sw.WriteLine("import arcpy");
                sw.WriteLine("from arcpy import env");
                sw.Write(script);
...

The problem is this makes debugging very difficult without viewing error messages produced in Python. How can I write errors to a log file or otherwise view the errors being produced by my ArcPy commands?

0

It is unclear how you are running your Python script from .NET, so I will assume that you are using System.Diagnostics.Process

Process proc = new Process()
{
    StartInfo = new ProcessStartInfo()
    {
        FileName = "C:\Python27\ArcGIS10.4\python.exe";
        Arguments = "<your python file>"
        RedirectStandardError = true; //error output
        RedirectStandardOutput = true; //other output
    }
}

proc.Start();
string all_error_output = proc.StandardError.ReadToEnd();  //read any errors
string all_normal_output = proc.StandardOutput.ReadToEnd();  //read any output
proc.WaitForExit();
0

An alternative solution was to access my cmd.py which passes the python command being processed and copying that command and running it in the python command window in ArcMap.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.