Select to view content in your preferred language

Get script folder from a script tool in a toolbox

1930
4
Jump to solution
02-05-2013 02:14 PM
LisaNelson
Emerging Contributor
Hello,

I have some custom ArcGIS toolboxes containing script tools (v10.0 and v10.1) written in Python.  Is there a way in Python to get the folder path of the script tool that is being executed?   I tried
os.getcwd()
but that returns the default path for the MXD in which the tool is running, which usually isn't in the same location as the script tool(s).  And, the
arcpy.env.workspace
property isn't an option for my scripts. 

I'm likely missing something obvious.  Any help appreciated!

Lisa
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
GeorgeNewbury
Frequent Contributor
I use something akin to:
import inspect, os  scriptName = inspect.getfile( inspect.currentframe() ) scriptFolder =  os.path.split(scriptName)[0]

View solution in original post

0 Kudos
4 Replies
curtvprice
MVP Esteemed Contributor
Hello,

I have some custom ArcGIS toolboxes containing script tools (v10.0 and v10.1) written in Python.  Is there a way in Python to get the folder path of the script tool that is being executed?  


Try:

import sys
import os
print sys.argv[0] 
print os.path.dirname(sys.argv[0])


Note this is the path of the python script - not the toolbox.
0 Kudos
GeorgeNewbury
Frequent Contributor
I use something akin to:
import inspect, os  scriptName = inspect.getfile( inspect.currentframe() ) scriptFolder =  os.path.split(scriptName)[0]
0 Kudos
LisaNelson
Emerging Contributor
Thank you both!  Either approach works!   I used this syntax:

os.path.split(inspect.getfile(inspect.currentframe()))[0]


Lisa
0 Kudos
curtvprice
MVP Esteemed Contributor
Never saw inspect used for that before. Cool.

I did a little experimenting to see what you get in different Python contexts.
In many situations inspect gives you more information.

# file test.py
import sys
import inspect
def x():
  print "inspect: ",repr(inspect.getfile(inspect.currentframe()))
  print "sys.argv[0]: ", repr(sys.argv[0])

x()


Running python script (like script tool):
D:\Users\cprice>C:\Python27\ArcGIS10.1\python.exe test.py
inspect:  'test.py'
sys.argv[0]:  'test.py'


Import:
D:\Users\cprice>C:\Python27\ArcGIS10.1\python.exe
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
inspect:  'test.py'
sys.argv[0]:  ''
>>> test.x()
inspect:  'test.py'
sys.argv[0]:  ''


It should be noted that imported modules have a __file__ property - handy if you're not sure which source of a module you imported...
>>> import test
>>> test.__file__
'test.py'



Interactive, pasting test.py at the >>> prompt (like pasting to ArcGIS Desktop python window):
The inspect method reports that your at the python prompt (<stdin>).
>>> import sys
>>> import inspect
>>> def x():
...   print "inspect: ",repr(inspect.getfile(inspect.currentframe()))
...   print "sys.argv[0]: ", repr(sys.argv[0])
...
>>> x()
inspect:  '<stdin>'
sys.argv[0]:  ''
>>>


And what if we're not sure which flavor of Python we picked up (say you've got multiple version of Python installed, say, x64 Desktop processing or the Enthought Python distribution, etc.)
>>> import sys
>>> sys.version
'2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]'
>>> sys.executable
'C:\\Python27\\ArcGIS10.1\\python.exe'


In the Desktop Python command line - you get this:
sys.version
'2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]'
>>> sys.executable
'C:\\ArcGIS\\Desktop10.1\\bin\\ArcMap.exe'


and.. inside Calculate Value in ModelBuilder:

def x():
  import sys
  import inspect
  f = "inspect: %s" + chr(10) + "sys.argv[0]: %s" + chr(10) + \
    "sys.version: %s" + chr(10) + "sys.executable: %s" 
  return f % \
     (repr(inspect.getfile(inspect.currentframe())), 
      repr(sys.argv[0]),sys.version,sys.executable)
--
Executing (Calculate Value): CalculateValue x() "def x():\n  import sys\n  import inspect\n  f = "inspect: %s" + chr(10) + "sys.argv[0]: %s" + chr(10) + "sys.version: %s" + chr(10) + "sys.executable: %s" \n  return f % \\n     (repr(inspect.getfile(inspect.currentframe())), repr(sys.argv[0]),sys.version,sys.executable)\n" Variant
Start Time: Wed Feb 06 10:37:05 2013
Value = inspect: '<string>'
sys.argv[0]: ''
sys.version: 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]
sys.executable: C:\ArcGIS\Desktop10.1\bin\ArcMap.exe
Succeeded at Wed Feb 06 10:37:05 2013 (Elapsed Time: 0.00 seconds)
0 Kudos