Incognito
Incognito

Reputation: 1953

Python: Help with UnboundLocalError: local variable referenced before assignment

I keep getting this error for a portion of my code.

Traceback (most recent call last):
File "./mang.py", line 1688, in <module>
files, tsize = logger()
File "./mang.py", line 1466, in logger
nl = sshfile(list, "nl")
UnboundLocalError: local variable 'sshfile' referenced before assignment

I haven't put the code up cause it goes back and forth between functions. I'm wondering if anyone could tell me why python is spitting this error? sshfile is not a variable it's a class.

Upvotes: 2

Views: 7237

Answers (1)

Mark Byers
Mark Byers

Reputation: 839214

You probably haven't imported the file which contains the definition of sshfile, or you need to qualify the class name with the package name. It depends on how you imported it.

What package does it come from? Where is it defined?


Update

For anyone else reading this, after a discussion in the comments it turned out that the problem was that the name sshfile had been used further down in the function as a variable name, like this:

class sshfile:
    pass

def a():
    f = sshfile() # UnboundLocalError here
    sshfile = 0

a()

The solution is to not use a variable name that hides a class name that you need to use.

Upvotes: 1

Related Questions