I am trying to put together a python toolbox (Arc 10.1) and would like to use a defined function in my code. The function is:

'sys' is imported at the top of the file    
def listdirs(folder):
        return [d for d in os.listdir(folder) if os.path.isdir(os.path.join(folder, d))]

It's a handy little bit of code I picked up on stackexchange that returns a list of folders in a given path.

I can't seem to figure out how to integrate it into the toolbox, everywhere I put it seems to disagree with the toolbox. Initially I had it in the def execute(self,parameters,messages) block. That didn't work, so I moved it outside of that and into the class level. I also tried putting it outside of the class.

The toolbox compiles without any errors (Using the 'Check Syntax' tool in the context menu in ArcCatalog) but when I run it my script fails the first time the function is called. No specific error is thrown, but my script exits through the except statement and doesn't finish the try block

Should I put it into an external file and then import that like I do arcpy,sys,traceback etc? Does anyone know if there are certain restrictions to using custom functions inside of a python toolbox and if so, what they are?

It seems that the real issue was that I was including sys and not os like I should have been. Rookie mistake. Anyhow, the script seems to be running nicely now, python toolboxes are great and are making my life much simpler.

Regarding the crux of my original question for any folks curious in the future - it doesn't seem to matter where you define a custom function in the python toolbox. As long as it has the classes that ArcGIS needs to know what to do with it anything else is ephemera.

  • 1
    Can you post any error messages you get beyond 'it doesn't work?' Commented Jan 16, 2013 at 1:31
  • 1
    You need to import os for the command you posted.
    – Jay Laura
    Commented Jan 16, 2013 at 1:59
  • As I said in the text of the question it does not throw an error - it simply exits and behaves like the script completed normally. I know this because the debugging info I have in the except clause prints and the script does not complete. It simply tells me the line number that the failure occurred on. @JayLaura - I'll check that out 1st thing tomorrow, although I'm pretty sure that it has worked with import sys in place of import os.
    – Kevin
    Commented Jan 16, 2013 at 2:30
  • Have you already seen this? How to Debug Python Toolboxes in 3 Easy Steps
    – blah238
    Commented Jan 16, 2013 at 3:24
  • 1
    It does throw an exception otherwise the code wouldn't reach the except clause. The except clause is hiding the actual error so move the code that isn't working out of the try block.
    – user2856
    Commented Jan 16, 2013 at 5:14

2 Answers 2

import arcpy
import sys
import os

class Tool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "My Tool"
        self.description = ""
        self.canRunInBackground = False

    def listdirs(self, folder):
        return [d for d in os.listdir(folder) if os.path.isdir(os.path.join(folder, d))]


Then, call it anywhere in the the tool (for example, in the execute method):


Posting as an answer because I can not post code in a comment. Swapping os for sys will likely solve your issue.

In [1]: import sys

In [2]: os.listdir('/')
NameError                                 Traceback (most recent call last)
/Users/jay/<ipython-input-2-f53adbb13cef> in <module>()
----> 1 os.listdir('/')

NameError: name 'os' is not defined

In [3]: import os

In [4]: os.listdir('/')


  • Well, that does look convincing... I'll check this out first thing tomorrow when I'm back at my office. Thanks!
    – Kevin
    Commented Jan 16, 2013 at 2:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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