How to access the tool path in the arcpy script?

08-14-2020 07:20 AM
New Contributor II

I have written a script for a tool (MyTool) in a toolbox (MyToolbox) used in ArcMap 10.6. In the script I like to get the path to the directory where the toolbox resides. Since we are distributing this tool, this path is a variable which will be different for each user.

For example if a user saves MyToolbox in C:\User\source\Workspaces directory, how can I get this directory in my script?

0 Kudos
2 Replies
MVP Esteemed Contributor

One option is to get the path to the running script.

import sys

script = sys.argv[0]  # ---- the running script‍‍‍

If you know where the script is relative to the toolbox, then you can work from there.

For instance, if you simply store the script(s) in the same location as the *.tbx, then you just parse the path out of the above.  If the script is located in a folder relative to the toolbox, then you parse of that folder name

And then there is

ListToolboxes—ArcGIS Pro | Documentation 

just limit the wildcard to your toolbox name

... sort of retired...
New Contributor III

For some reason sys.argv[0] wasn't working for me, so I used os.dirname(__file__) and that worked nicely

0 Kudos