0

We have a folder housing all custom Toolboxes/Models for our schema updates. We are attempting to automate these Toolboxes in upper environments. The initial problem is, I am unsure what Model name people are going to use.

How can I extract the Model name with Arcpy/Python then use this information with ap.GetParameterInfo?

The goal is to reset one required parameter in each environment.

Current failing code base:

import os
import arcpy as ap

_dir = r'D:\foo\bar' # folder with all the toolboxes

_lsdir = os.listdir(_dir)

for _tbx in _lsdir:
  _tb = ap.ImportToolbox("{0}\{1}".format(_dir, _tbx), "fakealias")
  print _tbx, _tb
  _p = ap.GetParameterInfo(_tb)
  for p in _p:
    print p.name, p.parameterType, p.value

Current Error output:

20190419_AddFC.tbx 'module 'fakealias' (built-in)'
AttributeError: Object: GetParameterInfo tool <module 'fakealias' (built-in)> not found.

I've successfully printed the ap.GetParameterInfo from a single Toolbox but only when hardcoding the Model name. This will not work long term.

Code formatting did not take on original publish.


This answered the initial question.

    import os
    import arcpy as ap

    _dir = r'D:\foo\bar' # folder with all the toolboxes

    _lsdir = os.listdir(_dir)

    for _tbx in _lsdir:
      _tb = ap.ImportToolbox("{0}\{1}".format(_dir, _tbx), "fakealias")
      print _tbx
      for _t in dir(_tb):
        if "__" not in _t:
          print _t
          _p = ap.GetParameterInfo(_t)
          for p in _p:
            print p.name, p.parameterType, p.value
  • 1
    try a raw string (forward slashes are generally bad unless you want escape characters): ap.ImportToolbox(r"{0}\{1}".format(_dir, _tbx), "fakealias") – Paul H Apr 23 at 17:43
  • I tested your suggestion and result is the same error. Even switching to '/' fails. – sstewart324 Apr 23 at 18:06
  • Use os.path.join to combine your paths instead of format. – BERA Apr 23 at 18:20
  • I tested your suggestion, results are the same. I'm thinking there has to be a step in between the getting "_tb" and GetParameterInfo. All the ESRI documentation shows a string value in GetParameterInfo("modelname"). – sstewart324 Apr 23 at 18:36
1

You're trying to get the ParameterInfo on the TBX, not a tool. This function works on a tool itself, not the higher level toolbox.

You can use a built in like dir to achieve your goal. You'll probably want to skip the dunder functions (if __ not in name):

for t in dir(_tb):
     if "__" not in t:
         print(t)
         _p = arcpy.GetParameterInfo(t)
         for p in _p:
            print p.name, p.parameterType, p.value
  • Actually I needed the dunder functions as there were other items under the dir(_tb) were returning other than the Model name. This worked. – sstewart324 Apr 23 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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