Select to view content in your preferred language

SetParameterAsText To View Python Tool Results

6207
32
06-08-2016 02:47 PM
DevinUnderwood2
Regular Contributor

I have the following which I can successfully get my desired results with the input prompt standalone script.  Yet, I prefer that a tool I created to be used and display the results. What am I missing in order to view the results?

#Import Modules
import csv,os,arcpy

#Set variables
ifile  = open('PythonShellSave.csv','r')
reader = csv.reader(ifile)
featureclass = input("Name of the feature class ? ")
#featureclass = arcpy.GetParameterAsText(0)
 
#Set path location of Excel (CSV) file 
path = 'C:\WriteMxdInfoToExcel'
os.chdir(path)

#CSV Module terminology row is horizontal & field is vertical;column is horizontal & row is vertical for excel.
for row in reader:
          for field in row:
              if field == featureclass:
                  print row [4] + " " + featureclass
                 
arcpy.SetParameterAsText(1,row)

#Close Excel (CSV) file
ifile.close()
0 Kudos
32 Replies
DanPatterson_Retired
MVP Emeritus

when you run a script, you should have a window open up that shows the results of the processing... someone turned it off... turn it back on... can't help with that one but it is there.  The window should not close until you are ready to close it.  The Results window is backup but it doesn't show the progress of the script and other fluff as it goes.  Rebecca, check it, I am on iThingy so can't help

0 Kudos
DanPatterson_Retired
MVP Emeritus

GOT IT

set to foreground processing... look at the figure here

Foreground and background processing—Help | ArcGIS for Desktop

DevinUnderwood2
Regular Contributor

Great advice, I just recently came across that article. I do see the results window, but it doesn't display the actual information I want.

I want to see the information that I can see when I run the script as a stand alone. The  python shell results is what I want to see in the above window for the tool.

Maybe the Add Message was never correctly displaying, does it differ from a print when viewing in the python shell ?

0 Kudos
DevinUnderwood2
Regular Contributor

Apparently you can't see AddMessage in python shell.

I commented out the AddMessage function. I used just arcpy.AddMessage which does work when testing with featureclass. I need to get it to work with final which I think it is having trouble being a looped result.

The code below produced the following in below results window.

#

Import Modules
import csv,os,arcpy

#Set variables
ifile  = open('PythonShellSave.csv','r')
reader = csv.reader(ifile)
#featureclass = input("Name of the feature class ? ")
featureclass = arcpy.GetParameterAsText(0)

#Set path location of Excel (CSV) file 
path = r'C:\Users\dunderwood\Documents\My Python Scripts\WriteMxdInfoToExcel'
arcpy.env.workspace = path

#Create Function
#def myMsgs(message):
 #   arcpy.AddMessage(message)
  #  print(message)

#CSV Module terminology row is horizontal & field is vertical;column is horizontal & row is vertical for excel
for row in reader:
          for field in row:
              if field == featureclass:
                  final = row[4] + " " + featureclass
                  #print(final)
                  #myMsgs(message)

arcpy.AddMessage('All Mxds associated with fc = {}'.format(featureclass))

#Close Excel (CSV) file
ifile.close()

0 Kudos
DevinUnderwood2
Regular Contributor

I am getting a returned value of None with the following excerpt of my code.

print viewresults + str(arcpy.AddMessage('All Mxds associated with fc = {}'.format(viewresults)))

What does None refer to ?

0 Kudos
DanPatterson_Retired
MVP Emeritus

you can't jumble them together

msg = "All Mxds associated with fc = {}".format(viewresults) 

print(msg)

arcpy.AddMessage(msg)

extra lines don't hurt and print only is used when the script is being run outside of an arctoolbox tool

and AddMessage is used to report tools results .... ie it is the toolbox equivalent of print

DevinUnderwood2
Regular Contributor

I removed the print statement.  I receive an error as follows.

Traceback (most recent call last):

  File "...\ReadMxdFCCSV.py", line 34, in <module>

    arcpy.AddMessage('All Mxds associated with fc = {}'.format(viewresults))

NameError: name 'viewresults' is not defined


Failed to execute (readmxdFC).

Despite previously setting viewresults as a variable

Any Idea why I am having this error ?

0 Kudos
DanPatterson_Retired
MVP Emeritus

with respect to the addmessage line, where is viewresults defined? if viewresults is being defined within a loop for instance, it may never be seen.  So to track down the error, put

viewresults = "view results not defined"

way up at the top of the script so that it always has a value.  If that message is returned, it is not being derived or returned elsewhere

0 Kudos
DevinUnderwood2
Regular Contributor

Yes it is on a loop.

for row in reader:
          for field in row:
              if field == featureclass:
                  viewresults = (row[4] + " " + featureclass)
0 Kudos
DanPatterson_Retired
MVP Emeritus

then do what I said since

if field == featureclass:

is never correct that is why it doesn't get a value. so put the line I suggeste before line 01 and run it again to see what I mean

print statements are cheap... throw them in even if you think something is going to work, it will save you hours of debugging

0 Kudos