Select to view content in your preferred language

SetParameterAsText To View Python Tool Results

6205
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

put

print(message)

inside the def after or before the adrcpy add message

get used to print statments using brackets is print("stuff")  get ready for python 3

use raw format in paths  (ie add an r  r"c:\mypath\some.txt"

ps

you will get a failure if you don't 'string' stuff

def myMsgs(message):

    """ a test """

    msg = "not another message... {0}".format(message)

    print(msg)

    arcpy.Addmessage(msg)

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

you don't need to import time since you are not using.

change  path = 'C:\Users\WriteMxdInfoToExcel'       to      path = r'C:\Users\WriteMxdInfoToExcel'

     (see dan's blog on file names)

I would add

     arcpy.env.workspace = path

and remove the os.chdir(path)

include for the arcy.AddMessage and the print in the myMsgs function or you will have to change each time.  Get used to using   print(message) with the parans since the next version of python will require it.  Again, Dan points to some good resources for what's coming.

Try those things and see if you get further.

....edit....what Dan said....

0 Kudos
DevinUnderwood2
Regular Contributor

Good points, I have used py 3 several times and had issues not being able to print until I learned about needing parenthesis. So good practice to use them in py 2 as well since it will work.

I would only use the r' raw for paths that were not for my pc, but I will include it every time now.

I made the other suggested changes and trying to grasp what Rebecca and Dan said to arrive at the edited code which states message has not been defined after running the script.

#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:\UsersWriteMxdInfoToExcel'
arcpy.env.workspace = path

#Create Function
def myMsgs(message):
    arcpy.AddMessage(msessage)
    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)
                 
#Close Excel (CSV) file
ifile.close()
0 Kudos
DanPatterson_Retired
MVP Emeritus

your first variable should be

message = ""    # two double or two single quotes

also

print(message)   # no spaces between print and the bracket

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

if you are going to us

     myMsgs(message)

as you show in line 25, then you need to have a

     myMsgs(final)

just link you are trying to print in line 24....which line 24 could be commented out.

the myMsgs(message)  assuming that the variable "message" is already set, and passes the value of it to the function.  if you want that line to stay as is, then add

     message = final

before that....assuming  "final" is a good variable.

DevinUnderwood2
Regular Contributor

Thank you for taking the time to further explain.  Will this work in a script that I am using with a tool I created? To automatically display message to results?  I tested out the function but it is not automatic when run and seen in python shell. However, as mentioned, my actual goal is to use it as a tool.

I will take a look at the Blog, thanks.

0 Kudos
DevinUnderwood2
Regular Contributor

I believe this reflects your comments.

#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)
                  message = final
                  myMsgs(message)
                 
#Close Excel (CSV) file
ifile.close()
0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Not running it myself, if it works in the python window, I would think it would run as a tool. One more warning, try to keep away from any spaces or special characters in file names/paths.  I may work, but it will come back to bite you at some points.  Also, keep filenames and field names short, i.e. not ThisIsAFieldNameForWhoKnowsWhat

How To Name Things In ArcGIS     in   Curtis Price's Blog 

0 Kudos
DevinUnderwood2
Regular Contributor

Make sense, I will make note of creating better naming conventions.

Interesting how when I run it  as a tool the results yield successful but the add message doesn't appear.

Maybe my tool parameters are set incorrectly.

The tool works fine as I use  a dropdown to navigate to location of featureclass. This is straight forward.

The output I am not entirely sure about.

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

hmm. Not sure off hand why the AddMessage wouldn't show up in results.  here i part of the results from one of my tools that is under construction....all those with the date/time tacked on the end are myMsgs

0 Kudos