Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8538887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:13:48+00:00 2026-06-11T11:13:48+00:00

I am trying to create a class in python which reads the access key/secret

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:13:49+00:00Added an answer on June 11, 2026 at 11:13 am

    You imported the dropbox.client module into a your module scope as client, but you also have a local variable client in 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 the client module 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 client in the function; you are trying to get the attribute DropboxClient from it, but you haven’t yet assigned anything to the variable client at that moment. So the UnboundLocal exception is thrown.

    The workaround is to either not use client as a local variable, to import the top-level dropbox module instead of it’s submodules, then refer to it’s submodules with the full dropbox.client, etc. paths, or thirdly, by giving the client module a new name:

    1. Don’t use client as a local:

      dbclient = client.DropboxClient(sess)
      # ...
      f, metadata = dbclient.get_file_and_metadata(file1)
      
    2. Import the dropbox module directly:

      import dropbox
      # ...
      
          sess = dropbox.session.DropboxSession(APP_KEY,APP_SECRET, ACCESS_TYPE)
          # ...
          client = dropbox.client.DropboxClient(sess)
      
    3. Provide an alias for the client module:

      from dropbox import session, rest
      from dropbox import client as dbclient
      # ...
      
          client = dbclient.DropboxClient(sess)
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create generic class in python which will do all the
I'm trying to create a class called Musician which inherits from my class Person
I am trying to create a class in VB.NET which inherits a base abstract
I can't create an utf-8 csv file in Python. I'm trying to read it's
I'm trying to create a python 2.7 tkinter module which uses scale widget data
I'm trying to create a GUI in Python using the Tkinter module, and part
I'm trying to write some python code which can create multipart mime http requests
I am trying to create a schedule class in python that takes the start
So I'm working in Python trying to create a ShapeSet instance that contains a
I'm trying to write a python code which create and initialize n timers in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.