Select to view content in your preferred language

How to determine in which context a python script is run?

1124
8
05-04-2010 09:52 AM
BenoitThierry
Emerging Contributor
Hello --

Python scripts can be used either as stand-alone script or as a script tool. Is there (programmatically speaking) a way to determine in which context a script is run?

Thanks

Benoit
0 Kudos
8 Replies
JamesHood
Regular Contributor
One method might be to look at the script itself.

The input parameters will be either hard coded or not.

If for instance you inputs look similar to this:  Input1 = "c:/temp/shape.shp"  or Input2 = "c/project1/point.shp"
you might conclude that the script can be run as a script.

However,  if the script contains inputs like these: Input1 = sys.argv[1]   and  Input2 = sys.argv[2]  etc.. 
you might conclude that the script is better suited as a script tool. 

Also maybe check to see if it has been loaded into an ArcToolbox already.  If so it has already been converted to a script tool.
0 Kudos
BenoitThierry
Emerging Contributor
Actually, my workflow is: (1) develop the script within Eclipse (with sys.argv), (2) debug it within Eclipse, (3) run it as a tool in ArcGIS or as a script in Windows command line.

As you see, my problem is to determine **at runtime** if the script is called as a tool from within ArcGIS or directly from PythonWin (or others). This would allow me to change the output formating/debug information displayed according to the context (for instance avoiding the use of gp.addmessage when run in command line mode). Your solution would only work to evaluate for which primary purpose the code has been developed (tool or script), right?

In other suggestion?

Benoit
0 Kudos
DanPatterson_Retired
MVP Emeritus
you can have it both ways by directing your messages to a list, then printing them when desired.  for example, you can use:

messages = []
messages.append("hello")
.....
messages.append("another message")
.....
etc
then use

messages.append("finished processing")
#print messages
printMessages(messages)

and it won't matter whether you are running the script from pythonwin or arctoolbox.  A note, if you terminate any process using sys.exit() make sure that you throw the printMessages line before exiting and before the geoprocessor (gp) is deleted


def printMessages(messages):
  '''provide a list of messages to this method'''
  for i in messages:
    print i
    gp.AddMessage(i)
0 Kudos
BenoitThierry
Emerging Contributor
One of the reason why I want to determine script context is to get a clean message output. As you can see from the included JPEG, gp.AddMessage provides a garbled text. Selecting the proper way of displaying a message (print in script context and gp.AddMessage in tool context) would allow me to avoid this problem.

But maybe is there another way of doing this I'm not aware of?

Benoît
0 Kudos
BruceHarold
Esri Regular Contributor
Hi

You might try importing sys and testing sys.executable, in process that is ArcMap.exe (or Catalog).

Regards
0 Kudos
AustinMilt
Deactivated User
I've wondered this same thing myself. Here's my (imperfect) solution applied to your problem:

def printMessage(msg):
  allModules = dir()
  if 'Tool Validator' in allModules: # Called from ArcMap
    gp.AddMessage(msg)

  else: # Called from command-line
    print msg

I'm not sure if this is what you were looking for, but it has worked for me, as I just wanted to change the command-line syntax of arguments. It's a flawed method because 1) There could be a separate module called Tool Validator already loaded into Python that was not loaded by ArcMap and 2) If you don't "run script in process" then the Tool Validator module is not loaded.

Hope this helps.
0 Kudos
AustinMilt
Deactivated User
Hi

You might try importing sys and testing sys.executable, in process that is ArcMap.exe (or Catalog).

Regards


Scratch what I said. use Bruce's method (as I am about to do).
0 Kudos
BenoitThierry
Emerging Contributor
This does it! Thanks!

Benoit

Hi

You might try importing sys and testing sys.executable, in process that is ArcMap.exe (or Catalog).

Regards
0 Kudos