Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Should be a simple question... but those can be the hardest. I have created a script that uses ESRI's arcpy site package and it functions properly in a stand-alone environment. I am now trying to integrate this script into a toolbox to be run within an open ArcMap session and I am having trouble. In integrating this script I am using the tool structure that ESRI suggests... . The script I have created has information in the 'ToolData' folder that I need to access to run the script.

So the question is how do I locate the script's directory dynamically when running it in an open ArcMap session? In my stand-alone script I use 'os.getcwd()'; this works because it returns the proper location of the script and I then know the structure around it. Whereas when incorporated as a script in a tool box on run within an ArcMap session 'os.getcwd()' returns the directory of the map document while the toolbox, script, and tool data are located where ever the user installed them and my script can't find this structure. Thanks for your help.

share|improve this question
Have you tried sys.argv[0]? – Chad Cooper Feb 6 '12 at 16:35
I think that would require the user to input the location in which they installed the toolbox every time they ran the script. It seems like there should be a better way. I install toolboxes in a common area and then forget about them. – Justin Mayers Feb 6 '12 at 16:47
Have you tried it? sys.argv[0] returns the path of the currently running module (script). __file__ should also do the trick. – blah238 Feb 6 '12 at 17:45
Thanks Chad for the correct answer and thank you @blah238 for making me try it when I thought it wouldn't work. – Justin Mayers Feb 6 '12 at 19:52
@JustinMayers - Great! I went ahead and put it in a answer, please mark it as accepted to close this question. – Chad Cooper Feb 6 '12 at 22:20
up vote 6 down vote accepted

Try sys.argv[0], on Windows it will return the full path to the currently running script.

share|improve this answer

Chad Cooper was correct in guiding me to the following statement to yield the scripts directory:

sys.argv[0] - gives full path and script name

or even more specifically to get just the location


Thanks to @blah238 for forcing me to look at something I thought wouldn't work. For additional info I also found support on ESRI's website:

share|improve this answer
Nice job Justin. It takes something to own up to one's mistakes publicly. It would have been easy to sidle sideways with a meek "oh!". Your summary also added useful info not previously mentioned. Welcome to! – matt wilkie Feb 7 '12 at 0:39

Your Answer


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.