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.


dryRun= 1
if dryRun:
    par= 'Hello'
    par= arcpy.GetParameterAsText(0)
# Do something

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

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

  • 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, 2013 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. Jul 17, 2013 at 21:33
  • Try to avoid testing for sys.executable in this situation. The Python specifications (docs.python.org/3/library/sys.html#sys.executable) defines that sys.executable is the path to the Python interpreter or None. ArcMap.exe is not an interpreter since you cannot execute Python scripts with ArcMap.exe script.py. However, yes, it works but only because Esri seems not to care about specifications and standards. It should return None. To be fair, there is an issue with embedded Python environments. QGIS and other tools have that issue too.
    – Thomas
    Oct 27, 2021 at 19:24
  • NB. ArcGIS Pro also returns the path to C:\Program Files\ArcGIS\Pro\bin\ArcGISPro.exe (itself) and not None.
    – Thomas
    Oct 27, 2021 at 19:31
  • This is a product of the fact that the system module defaults to argv[0] as sys.executable and not on the part of any malice or neglect on the part of Esri. Nov 2, 2021 at 18:20

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)
    # 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).

  • well, this kinda works for now b/c I do not pass arguments but is there any more "elegant" way?
    – Mahdi
    Jul 1, 2013 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, 2013 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, 2013 at 5:44

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'
  • 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, 2013 at 19:32
  • What else do you need to change besides the parameters if the script is ran from outside ArcMap?
    – Paul
    Jul 1, 2013 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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