Select to view content in your preferred language

ArcGIS Pro Python script does not run and no errors given as to why.

3742
3
Jump to solution
03-14-2023 09:14 PM
GregAO
by
Occasional Contributor

I am running ArcGIS Pro 2.9. I have converted a Python 2.x script to Python 3 using the built-in tool "Analyse Tools for Pro" - only the 'print' statements have been changed.

Added the modified script to the Toolbox I previously had from Arcmap 10.6.

Ran script (from toolbox in Arc Pro), and it did not run. No messages of any sort were given as to why the script did not work.

The script, (attached), uses the geoprocessing tool RasterFocalMean.

Am I doing anything the wrong way?

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
Brian_Wilson
Honored Contributor

I realize this script says "Author: Esri" but that does not mean it's well written. Ha.

Break up the big "try" block into many functions. Never wrap the whole thing in one try block. In fact it's usually easier to get rid of the try block and let the code crash and tell you what line it was on when it crashed. This can backfire if it's inside the Esri code but most often points at some stupid typo.

They put everything in a try block specifically to hide the error messages from you to make it look all bright and shiny. We out here in the trenches WANT the ALL the error messages.

When using any try block (if you must)  always print out the exception like this

 

 

try:
   a = b[0]
except Exception as e:
   print("Exception", e)

 

 

Of course add whatever else you want like arcpy.AddMessages() or whatever. But the contents of "e" is invaluable.

Always set up to run the script in a debugger like VS Code or Spyder. Using functions helps a lot here too. You can create test cases for each function and test them independently in the debugger.

In your case after adding the "Exception" and running your code in VS Code, I saw this

EXCEPTION module 'string' has no attribute 'split'

"string" module ---- I never use that and don't want to learn. 🙂 That whole line 53 looks perilous to me.  There is no "split" method. Maybe it's leftover from Python 2.x??

    inFeatureClasses = string.split(re.sub("'""","",arcpy.GetParameterAsText(0)), ";")

 

The regular expression in there is just torture to me and I don't want to look at it for you.

It looks like it's doing something with quotes and then returning an unquoted list based on using a semi-colon as a separator.

Ignoring it, get rid of "string.split"

Using just split, instead,

 

 

param = arcpy.GetParameterAsText(0)
fcs = re.sub("'""", "", param) # Whatever this does
inFeatureClasses = fcs.split(';')

 

That might fix the whole script, I doubt it, but you have to fix one thing at a time...

Imagine a world in which Esri implemented best practices designed to make more robust code and to help end users instead of hiding mistakes.

View solution in original post

3 Replies
DanPatterson
MVP Esteemed Contributor

It ran, what what are the Messages  from the tool dialog

 


... sort of retired...
0 Kudos
Brian_Wilson
Honored Contributor

I realize this script says "Author: Esri" but that does not mean it's well written. Ha.

Break up the big "try" block into many functions. Never wrap the whole thing in one try block. In fact it's usually easier to get rid of the try block and let the code crash and tell you what line it was on when it crashed. This can backfire if it's inside the Esri code but most often points at some stupid typo.

They put everything in a try block specifically to hide the error messages from you to make it look all bright and shiny. We out here in the trenches WANT the ALL the error messages.

When using any try block (if you must)  always print out the exception like this

 

 

try:
   a = b[0]
except Exception as e:
   print("Exception", e)

 

 

Of course add whatever else you want like arcpy.AddMessages() or whatever. But the contents of "e" is invaluable.

Always set up to run the script in a debugger like VS Code or Spyder. Using functions helps a lot here too. You can create test cases for each function and test them independently in the debugger.

In your case after adding the "Exception" and running your code in VS Code, I saw this

EXCEPTION module 'string' has no attribute 'split'

"string" module ---- I never use that and don't want to learn. 🙂 That whole line 53 looks perilous to me.  There is no "split" method. Maybe it's leftover from Python 2.x??

    inFeatureClasses = string.split(re.sub("'""","",arcpy.GetParameterAsText(0)), ";")

 

The regular expression in there is just torture to me and I don't want to look at it for you.

It looks like it's doing something with quotes and then returning an unquoted list based on using a semi-colon as a separator.

Ignoring it, get rid of "string.split"

Using just split, instead,

 

 

param = arcpy.GetParameterAsText(0)
fcs = re.sub("'""", "", param) # Whatever this does
inFeatureClasses = fcs.split(';')

 

That might fix the whole script, I doubt it, but you have to fix one thing at a time...

Imagine a world in which Esri implemented best practices designed to make more robust code and to help end users instead of hiding mistakes.

GregAO
by
Occasional Contributor

Thanks Brian, your information is very useful to me. I will break it up into small chunks & test individually. The original ESRI script was modified by a workmate of mine since retired, and yes, parts of it a quite cryptic!

I don't think I'll delve into Spyder or the like just yet.

0 Kudos