68380

UnboundLocalError: local variable 'url' referenced before assignment

Question:

I'm trying to get this code working, but it keeps coming up with the error in the title. I don't get it. The function "url" is set before the "get_media" function, and the same function call thing works with other functions I've set, but it says otherwise. I've looked at similar question's answers, but I cannot understand any of them, because the answers are designed around their complicated code, and they offer up no proper explanation as to how it works.

def url(path): if path.find("?") != -1: pre = "&" else: pre = "?" return protocol +"://" +host +base_path +path +pre +"access_token=" +access_token def get_media(insta_id, max_id=None): insta_id = str(insta_id) path = url("/users/%s/media/recent/") # ERROR COMES UP HERE if max_id is not None: path = path +"&max_id=%s" % max_id url = urllib.request.urlopen(path) url = url.read().decode("utf-8") url = json.loads(url) return url

Any help appreciated. Tell me if you need more code to work with.

B

Answer1:

You assign to a local variable called "url" later in your function. Because of that, Python treats <em>every</em> reference to "url" within that function as local. But of course you haven't defined that local variable yet, hence the error.

Use a different name for the local "url" variable. (It's never a URL anyway, so you should definitely use a better name.)

Recommend

  • AudioKit: Noise gate
  • Accessing IP restricted URI from Azure function (powershell)
  • How to create the new target in Xcode for app extension using CMake?
  • Source file (.py) not compiling (.pyc not updated)
  • MySpace DOM?
  • After message type e, program doesn't return to selection-screen ABAP
  • Embed multiple jpeg images into EXCEL programmatically?
  • SEO friendly 301 redirect .htm to .aspx
  • Can I call custom javascript from an R jupyter notebook
  • VB.Net Double comparison after some additions
  • How to select table rows/complete table?
  • How many Vertica Databases can run on a Host in the same time?
  • Zeromq with python hangs if connecting to invalid socket
  • How do you create a Fuseki SPARQL server using the Apache Jena Java API?
  • Get used tables from sql query [duplicate]
  • What is the use of a session store?
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • how to set variables in a php include file?
  • blocking spam referer domain with web.config
  • Installing Apache MyFaces 2 on WildFly 8.2.0
  • How to define and use opencv mat of user type
  • Extracting HTML between tags
  • Using jQuery closest() method with class selector
  • Array.prototype.includes - not transformed with babel
  • Javascript Callbacks with Object constructor
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • vba code to select only visible cells in specific column except heading
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • Proper folder structure for lots of source files
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • Bitwise OR returns boolean when one of operands is nil
  • Is there any way to bind data to data.frame by some index?
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?