UnboundLocalError: local variable 'resp' referenced before assignment error using Juypter Notebook

2225
8
05-29-2019 06:35 AM
BennettMorris
New Contributor II

Hello,

I recently got a new work computer and I have a Juypter notebook that I have been using for some time and it's no longer working.  My python code logs into our Portal so I can run other reports.  I can log onto AGOL using this script, but not our Portal any more.  Here is the code:

from arcgis.gis import GIS
from IPython.display import display
from getpass import getpass
import sys
import datetime
import time


ArcGISwebsite = input("Enter ArcGIS Portal / AGOL URL: ")
username = input("Enter user name: ")
password = getpass("Enter password: ")
g = GIS(ArcGISwebsite, username, password)

if g.properties.isPortal == True:
   print("\nConnected to: {}\n\nConnected as: {}".format(g.properties.name + " (" + g.properties.portalHostname +")", g.users.me.username))
else:
   print("\nConnected to: {}\n\nConnected as: {}".format(g.properties.name + " (" + g.properties.urlKey + "." + g.properties.customBaseUrl +")", g.users.me.username))
pass

I receive the "UnboundLocalError: local variable 'resp' referenced before assignment" at "g = GIS(ArcGISwebsite, username, password)".  I'm running Windows 10 Pro, ArcGIS Pro 2.3.3, ArcMap 10.7. Any help would be greatly appreciated.  Thanks!

0 Kudos
8 Replies
MikeButt
New Contributor II

We have the same problem.  We have tracked it down to requests being made to the server in connection.py of the ArcGIS rest API.  The parameter to set the response to return JSON (f=json) is being ignored so when the python tries to parse the JSON it fails as it is HTML.  However, this is hidden by the outer try catch.  No idea why the parameter is being ignored.

When we hit the endpoint from postman or any other tool the parameter is respected and the server returns json.

BennettMorris
New Contributor II

Hi Mike,

Did you change the connection.py file so that it does work?  I can login to my Portal (using the code above) by using the admin connection https://FQDN:7443/arcgis and then using the builtin original admin account and password.  I have a ticket with ESRI about this too.  

0 Kudos
MikeButt
New Contributor II

Hi Bennett,

We added some debug info into connection.py to check what it was doing and noticed it was returning HTML. Our infrastructure people suggested using the admin connection and FQDN and it worked. We now believe it might be something to do with the url in our load balancer being different to our portal context URL.  We are still investigating though. 

Mike

0 Kudos
BennettMorris
New Contributor II

Hi Mike,

I was on the phone with ESRI yesterday and the issue is with Microsoft Single Sign On (SSO) and the DNS alias for our ArcGIS Enterprise.  If I use https://<ActualServerName.Domain.com>/<PortalWebAdapter> it works with my Microsoft active directory account information and SSO, but if I use https://<DNSalias.Server.com/<PortalWebAdapter> it doesn't work.  Something with the DNS alias and the Python API.  I'll let you know what ESRI comes back with.

0 Kudos
MichaelAugust
Occasional Contributor III

Running into what I think is the same problem - did anyone ever get back to you?

0 Kudos
BennettMorris
New Contributor II

I had an open ticket with ESRI, but it was not resolved.  I'm still trying to figure it out.  I think it might be something with the web adapter, but that's just a guess.

0 Kudos
JeffreyWilkerson
Occasional Contributor II

I started having this issue when I took the GetParameterAsText away from a parameter that was defined as an output, derived parameter.  To be clear, I started writing this script as having a user defined output parameter for an Excel file output, but then figured it would be better to have it as a defined output parameter. 

At the top of my script I always had this:

Out_Excel_File = arcpy.GetParameterAsText(1)
 if Out_Excel_File == '#' or not Out_Excel_File:
 Out_Excel_File = "\\\\w-gis-portal\\e$\\Projects\\DRT\\InExcel_Layer_TableToExcel.xls" # provide a default value if unspecified

But at the bottom of my script I started using this:

arcpy.TableToExcel_conversion(InExcel_Layer, Out_Excel_File)
arcpy.SetParameterAsText(1, Out_Excel_File)

So I figured I didn't need the first 2 lines where I set Out_Excel_File at the top of the script, and I remarked them out.  But doing that triggered an 'UnboundLocalError'.  Resetting them as active code allowed the script to run without error though, so I'm moving forward with what you see here.

Hope this helps someone.

0 Kudos
warrenfel
New Contributor

All variable assignments in a function store the value in the local symbol table; whereas variable references first look in the local symbol table, then in the global symbol table, and then in the table of built-in names. Thus, global variables cannot be directly assigned a value within a function (unless named in a global statement), although they may be referenced.

The unboundlocalerror: local variable referenced before assignment is raised when you try to use a variable before it has been assigned in the local context. Python doesn't have variable declarations , so it has to figure out the scope of variables itself. It does so by a simple rule: If there is an assignment to a variable inside a function, that variable is considered local .

 

0 Kudos