I am trying to create a class in python which reads the access key/secret for dropbox and then downloads a file. The key/secret part is working alright, but I seem to be having a problem recognizing the client object, probably due to an issue with global vs local variables. I can’t find my answer anywhere else.
Here’s part of my code:
from dropbox import client, rest, session
class GetFile(object):
def __init__(self, file1):
self.auth_user()
def auth_user(self):
APP_KEY = 'xxxxxxxxxxxxxx'
APP_SECRET = 'xxxxxxxxxxxxxx'
ACCESS_TYPE = 'dropbox'
TOKENS = 'dropbox_token.txt'
token_file = open(TOKENS)
token_key,token_secret = token_file.read().split('|')
token_file.close()
sess = session.DropboxSession(APP_KEY,APP_SECRET, ACCESS_TYPE)
sess.set_token(token_key,token_secret)
client = client.DropboxClient(sess)
base, ext = file1.split('.')
f, metadata = client.get_file_and_metadata(file1)
out = open('/%s_COPY.%s' %(base, ext), 'w')
out.write(f.read())
And here’s the error:
Traceback (most recent call last):
File "access_db.py", line 30, in <module>
start = GetFile(file_name)
File "access_db.py", line 6, in __init__
self.auth_user()
File "access_db.py", line 20, in auth_user
client = client.DropboxClient(sess)
UnboundLocalError: local variable 'client' referenced before assignment
I’m new-ish to python so let me know if there are other obvious things I may be doing wrong.
You imported the
dropbox.clientmodule into a your module scope asclient, but you also have a local variableclientin your.auth_user()method.When python sees an assignment (such as
client =) in a function when compiling, it marks that name as a local variable. At this point your import of theclientmodule is doomed, it is no longer visible in your function under that name.Next, in python’s eyes you are trying to access that local variable
clientin the function; you are trying to get the attributeDropboxClientfrom it, but you haven’t yet assigned anything to the variableclientat that moment. So theUnboundLocalexception is thrown.The workaround is to either not use
clientas a local variable, to import the top-leveldropboxmodule instead of it’s submodules, then refer to it’s submodules with the fulldropbox.client, etc. paths, or thirdly, by giving theclientmodule a new name:Don’t use
clientas a local:Import the
dropboxmodule directly:Provide an alias for the
clientmodule: