Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

For debugging purposes I have created a hardcoded conditional that checks it the script is run as stand alone or as an ArcGIS tool. Is there anyway to fugure this out at run time? e.g. some environment variable, global variable, etc.

e.g.

dryRun= 1
if dryRun:
    par= 'Hello'
else:
    par= arcpy.GetParameterAsText(0)
# Do something
share|improve this question
add comment

3 Answers 3

Check sys.executable.

In the Python window:

>>> import sys
>>> sys.executable
'C:\\Program Files (x86)\\ArcGIS\\Desktop10.2\\bin\\ArcMap.exe'

and then in Python.exe:

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.executable
'C:\\Python27\\ArcGIS10.2\\python.exe'

So a simple if os.path.basename(sys.executable).lower() == 'python.exe' should do it.

share|improve this answer
    
Thanks, but my main goal was to check between local run vs. server run (i.e. as a geoprocessing service- i think that is the name) –  Mahdi Jul 17 '13 at 20:57
    
I believe that sys.executable will be something different when run as a server tool as well. Write a test script that issues arcpy.AddMessage("Executable: " + sys.executable) to confirm. –  Jason Scheirer Jul 17 '13 at 21:33
add comment

If I understand your question correctly, you want to check to see if you are running a script as a script tool or not. If it is running as a script tool, you want to set some variables using GetParameterAsText, and if it is running as standalone, you want to use some hard coded variables, is this correct? If so, you can do the following:

if len(sys.argv) > 1:
    #Assign input and output parameters for script tool
    par = arcpy.GetParameterAsText(0)
else:
    # Hard coded parameters for testing
    par = 'Hello'

By checking the length of sys.argv you are determining how many parameters are being passed to the script. There is always at least one parameter (the location of the script itself), so if there is more than one being passed, you can assume that ArcGIS is sending them (unless you happen to be passing arguments from somewhere else).

share|improve this answer
    
well, this kinda works for now b/c I do not pass arguments but is there any more "elegant" way? –  Mahdi Jul 1 '13 at 19:29
    
If by elegant, you mean shorter, you can make it a one-liner like so: par = arcpy.GetParameterAsText(0) if len(sys.argv) > 1 else 'Hello' –  Paul Jul 1 '13 at 19:36
    
by "elegant" i mean something like checking an environment variable, etc. Not "elegant" in python sense. BTW, while I use this structure myself a lot, in the case we are discussing, we are most likely setting many params for the script, so that short-ifs will not be really elegant. –  Mahdi Jul 21 '13 at 5:44
add comment

A simple way to do it would be like this:

import arcpy
par = arcpy.GetParameterAsText(0)
if not par: #If no parameter has been passed, an empty string is returned
  par = 'Hello'
share|improve this answer
    
I did not try it before, but I thought it would raise an exception, this is definitely interesting... yet, it will not let me to rewire part of the code logic based on stand alone vs. tool. –  Mahdi Jul 1 '13 at 19:32
    
What else do you need to change besides the parameters if the script is ran from outside ArcMap? –  Paul Jul 1 '13 at 19:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.