Get path to script tool inside of a toolset?

232
7
01-12-2022 12:50 PM
GB_MattPanunto_GISS
Occasional Contributor

I've used sys.argv[0] before to get the path to the current script tool being ran. However, I'm finding that if the script tool is inside of a toolset within the toolbox, sys.argv[0] doesn't return anything. Is there a way to get the script tool path when it is inside of a toolset?

0 Kudos
7 Replies
DanPatterson
MVP Esteemed Contributor

 

# --- carry on from before
import arcpy
pth = r"C:\arcpro_npg\npg_tools.tbx"
t0 = arcpy.ImportToolbox(pth)
t0.__pathname__
'C:\\arcpro_npg\\npg_tools.tbx'

# -- or
tools = arcpy.ListTools('*_npg')
tools
['AttributeSort_npg',
... snip ...
 'Triangulate_npg']

t0.__pathname__
'C:\\arcpro_npg\\npg_tools.tbx'
c = arcpy.AttributeSort_npg.__code__  # --- get the code reference
c.co_filename
'C:\\arcpro_npg\\npg_tools.tbx'

# ----
# -- ..\tbx_tools.py

 

It stops at some point.  Once I get the toolbox name from either of the methods.  I know where the script is located since I store my scripts relate to the tbx path like

..\\tbx_path\\scripts\

and you can parse off the module name (_npg) from the tool name (arcpy.AttributeSort_npg)


... sort of retired...
0 Kudos
GB_MattPanunto_GISS
Occasional Contributor

But what if you don't know the path to the toolbox? Say if the toolbox is being used by another user on a different machine.

0 Kudos
DanPatterson
MVP Esteemed Contributor

see lines 3, 4, 5  import the toolbox BUT that is as far as it goes.

The new format of toolboxes ( *.atbx ) has some more options that I have been exploring


... sort of retired...
0 Kudos
GB_MattPanunto_GISS
Occasional Contributor

Hmm, but wouldn't importing the toolbox require the user to specifically define its location first? I'm trying to avoid that.

The end goal here is to determine the path to the toolbox on the user's machine, without them having to define its location. This was easy with sys.argv[0], since I could just grab the parent directory of the script tool, but this method doesn't seem to be working for me when the script tool is inside of a toolset.

I'd be willing to convert the toolbox to the new format (.atbx), if that made this possible.

0 Kudos
DanPatterson
MVP Esteemed Contributor

If you don't know where the user stores their toolboxes, then you will have a difficult job.

import sys
sys.path
['C:\\arc_pro\\Resources\\ArcPy',... snip ...]

# -- my stuff
# 'C:\\Arc_projects', 'C:\\arcpro_npg',

which isn't going to help unless you want to remove path info normally present from that on a specific machine


... sort of retired...
0 Kudos
DonMorrison1
Occasional Contributor III

You can use the python '_file_' variable to get the path to your .py file.  From there you can back it up to the root of your toolbox.   Here is an example of how I do it:

 

path_code_base = os.path.normpath(__file__ + '/../../../../')

 

GB_MattPanunto_GISS
Occasional Contributor

I discovered that if you embed the script AND put a password on it, sys.argv[0] will include the toolbox in the path of the script tool. A bit of a clunky workaround, but may work in a pinch.

0 Kudos