Running python from cmd window works well, how to run from toolbox???

2068
5
Jump to solution
11-24-2014 06:08 PM
BenVan_Kesteren1
Occasional Contributor III

Hi All,

I have a python script that I have just written and I am able to run it quite simply by opening the CMD window, and typing >>>python myScript.py and it runs great, it shows me all my print commands as it runs through.

Now I have it working I want to add it to my Toolbox so I can simply run the script once a week to do what I have set it to do.

I have quite simply right clicked the toolbox, and selected ADD | Script... I then get it to point at my myScript.py file, and thats it.

So then I go back to the script in my catalog, and double click it and it runs. But unfortunately I am unable to see any of the Print statements as its the esri window, it only lets me know when its complete.

Basically see the first screenshot of the text the script prints out, this is what I like...

2014-11-25_12-58-31_CWindowssystem32cmd.exe - python  Importing_CAD_Data.py.jpg

See this second screen shot, this is the ESRI version, I am hoping to get all my print statements to show in this window... is it at all possible??

2014-11-25_13-08-06_View Connected Users.jpg

Thanks heaps for your time.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
OwenEarley
Occasional Contributor III

Check out the AddMessage function in arcpy: ArcGIS Help 10.1

Before you return your user count you could modify the print statements to this:

if total_users > 0:
     arcpy.AddWarning("Total number of connected users " + str(total_users))
else:
     arcpy.AddMessage("No users connected")

View solution in original post

5 Replies
BenVan_Kesteren1
Occasional Contributor III

Just to give you a better idea of my problem, the below script returns a list of Users currently connected to my SDE... this script is only useful if i run it from the CMD window, if I run it from Catalog as a script it displays the window shown in my first post.. and as you can see it does not return a list of connected users.

Cheers

#-------------------------------------------------------------------------------
# Name:        Find current users connected to SDE
# Purpose:      This script is specifically written to be able to return a
#               list of all current users connected to the SDE_Spatial database.
#
# Author:      Ben Van Kesteren & Nathan Duncan
#
# Created:     14/11/2014
# Copyright:   (c) Ben Van Kesteren 2014
#
#-------------------------------------------------------------------------------


import arcpy
import csv


def main():
    PCNameKey = pc_name_key()
    UserList = user_list()
    outputFinal = output(PCNameKey, UserList)
    for name in outputFinal:
        print name
  print name
    print "\n" + str(outputFinal)+ "\n"
    
## Creates a dictionary from the listed CSV file
def pc_name_key():
  reader = csv.reader(open('H:\\GIS Admin\\PCNumbers.csv'))
  key_dict = {}
  for row in reader:
  key = row[0]
  if key in key_dict:
  pass
  key_dict[key] = row[1]
  return key_dict




def user_list():
  # Set the admistrative workspace connection
  arcpy.env.workspace = "H:\\ESRI SDE Connections\\GISADMIN@SDE_Spatial@Smithy.sde"
  user_list = arcpy.env.workspace


  # Create a list of users
  '''
  NOTE: When the arcpy.env.workspace environment is set, a workspace
  does not need to be provided to the function.
  '''
  users = arcpy.ListUsers(user_list)


  # Create a list of SDE connected usernames.
  id_users = [user.ClientName for user in users]
  current_users = []
  for user in id_users:
     if user != str(u'gisserver'):
         user = user.upper() # set PC Numbers to uppercase
         user = str(user)
         if user in current_users:
             pass
         else:
             current_users.append(user)
     else: pass


  # Count all users currently connected
  total_users = len(current_users)

  # Print the total number of users currently connected
  if total_users > 0:
       print "\nTotal number of connected users " + str(total_users) + "\n"
  else:
       print "\nNo Users Connected\n"
  return current_users




def output(PC_Key, name_list):
    output_list = []
    for id in name_list:
        if id in PC_Key:
            output_list.append(PC_Key[id])
        else: pass
    return output_list


## Boilerplate
if __name__ == '__main__':
  main()
0 Kudos
OwenEarley
Occasional Contributor III

Check out the AddMessage function in arcpy: ArcGIS Help 10.1

Before you return your user count you could modify the print statements to this:

if total_users > 0:
     arcpy.AddWarning("Total number of connected users " + str(total_users))
else:
     arcpy.AddMessage("No users connected")
BenVan_Kesteren1
Occasional Contributor III

Hi Owen, thanks very much for that, has worked well for this little script.

So are you saying that print statements are not recognised by this ESRI window at all? I need to use AddMessage and AddWarning instead from now on?

Thanks again.

0 Kudos
curtvprice
MVP Esteemed Contributor

That's correct. Script tool messages can only appear in geoprocessing results if you use the arcpy methods to send those messages. On the plus side, these messages are not only printed, they are saved as results in the Results window for later review.

curtvprice
MVP Esteemed Contributor

Posting Code blocks in the new GeoNet

The above link shows how to post nicely highlighted and numbered python code on the forum.

While I'm here, I can't resist spreading the gospel on text formatting -- much better than adding strings together with + ...

arcpy.AddWarning("Total number of connected users {}".format(total_users))

7.1. string — Common string operations — Python 2.7.8 documentation