views:

287

answers:

1

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.

+1  A: 

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.

Mark Byers
its defined right below the logger function. Its not part of a package, its a function i coded. I dont know what you mean by qualify the class name. This code used to work fine till i made some changes, but I never touched this part of the code.
Incognito
It's a function you coded? Before you said it was a class. It would be really useful if you could extract the relevant lines of code into a new file, maybe make a 10 line simplest possible self-contained example that demonstrates the error, and post that. Remove all code that is not necessary to demonstrate the error.
Mark Byers
I was about to post some stripped down code, except that the error doesnt show up in the stripped down version.
Incognito
@Incognito: then the error lies in the other code. Make an example using that. Find out what part of the code you need to add/remove to make the error come and go. It can take a while to home in on the exact line that gives the problem, but it's faster to do that than for us to guess randomly without being able to see the code.
Mark Byers
Is it possible to narrow the problem to functions? The code within the class hasn't been touched, only the logger function.
Incognito
@Incognito: Make a new file containing your full code. Run it and verify that the error is there. Now remove all unused code. Run it again and verify the error is still there. Now remove more code (or replace it with something simpler, e.g. replace `x=foo(bar)` with `x=0`), verify. Remove. Verify. If you remove code and the error goes away, put that code back and try to remove something else. Once you have some code that no line can be removed without fixing the error, post it here.
Mark Byers
OK! i found what the error was. Really stupid one too, I accidently created a variable lower down that had the same name as the sshfile class.
Incognito
@Incognito: Yes, I was sort of hoping that by doing this, you'd be able to solve it yourself. It often happens that some bug looks impossible to explain until you reduce it to the simplest example, and then it becomes obvious. Thanks for letting us know what the problem was.
Mark Byers