2

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.

3
  • 2
    You're going to have to post at least some of the code if you want anything other than wild guesses. Commented Feb 13, 2010 at 0:57
  • If the class definition doesn't exist in the scope where you use it, Python will call it a local variable. So the problem is that somehow that class definition didn't happen. Commented Feb 13, 2010 at 0:59
  • Covered by "UnboundLocalError: local variable 'player' referenced before assignment" Commented Dec 24, 2021 at 14:06

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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.
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.
@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.
@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.
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.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.