Select to view content in your preferred language

arcpy unicode error in VS Code debugging mode

1987
9
Jump to solution
03-23-2023 10:52 AM
ShawnLanning
Emerging Contributor

I recently began having issues using the debugger in Visual Studio Code.  It has always worked in the past (and still does except with arcpy).  Now every time I try to import arcpy I get a "name 'unicode' is not defined" error that is coming from _base.py.  I am using python 3.9.16 64 bit interpreter and have the most current version of arcgis pro installed.  I have tried to uninstall VS Code and ArcGIS Pro both and the same issue persists, but the second I comment out the import arcpy the debugger works fine (until it gets to something that needs the arcpy lib obviously).  Any thoughts?  I really like the VS Code IDE and would like to continue using it but am also open to similar easy to use options if anyone has any that might solve my issue.

 

ShawnLanning_0-1679593764432.png

 

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
NatKrase
New Contributor

You likely have 'Raised Exceptions' enabled as a breakpoint. Uncheck it and your script should run as expected.

image.png

The NameError is being raised because unicode is not defined, but it is handled by the except clause. @ShaunWalbridge mentioned, that try statement is likely unnecessary at this point. However, it isn't actually causing any problems with your script.

View solution in original post

Tags (2)
9 Replies
ShaunWalbridge
Esri Regular Contributor

Did you do any specific configuration to add more verbose warnings or other configuration changes? I believe that VSCode + ArcPy is a common combination in use, and the `unicode` call, while failing, is expected to because it is wrapped in a try/except block which will raise on Python 3. These should ultimately be removed from the ArcPy code in the future, but I wouldn't expect a Python import to fail on them. You could check if only your code is set to debug, but also try making a dummy script with the same try / except lines as you're seeing raise, and see if that also fails.

Cheers, Shaun

0 Kudos
ShawnLanning
Emerging Contributor

Thank you for the reply.  In the past I haven't had any issue with debugging.  I haven't changed any configurations and have tested with a script I know works and in the past was able to debug.  Currently I am trying to test with the simplest test script possible and I still get the same error.  The code I am trying currently is  (also note I have tried running just the top 3 statements and it also bombs and returns the unicode error):

import os

import arcpy

print ("hi")

# Set workspace
#workspace = r'Database Connections\Colter3_UWADMINGIS_PPL.sde'  # new colter3 db
workspace = r'Database Connections\reo-db_6000_UWADMINGIS_PPL.sde'  # new reodb

# Set the workspace environment
arcpy.env.workspace = workspace
0 Kudos
ShaunWalbridge
Esri Regular Contributor

Right, unfortunately that script still imports ArcPy, so it doesn't help determine whether the problem affects all Python code or is specifically within ArcPy. Can you see if this script runs successfully?

 

try:
    unicode
except:
    print("In Python 3, all strings are unicode")

 

 

If that also fails, it lets us know that the issue is affecting all Python code. If it prints the output, then its something in the relationship with ArcPy specifically.

Cheers, Shaun

0 Kudos
ShawnLanning
Emerging Contributor

Shaun - Thanks again for the suggestions.  Below is 

  • If I debug that code by itself it fails because of a syntax error since unicode isn't defined. 
  • If I try to debug it with arcpy imported the code bombs at the import arcpy level
  • If I debug any code that doesn't involve importing arcpy the process works successfully.
0 Kudos
ShaunWalbridge
Esri Regular Contributor

OK, if that script doesn't print In Python 3, all strings are unicode then it indicates there is some broader issue with the debugging configuration of VSCode. I would double check the Python interpreter configuration, and perhaps try creating a separate clean profile to see if you can reproduce the error there as well and isolate any possible configuration changes. Unfortunately, if the basic Python configuration doesn't respect the normal exceptions model, there's not much we can do on the ArcPy tier to handle the problem.

Cheers, Shaun

0 Kudos
NatKrase
New Contributor

You likely have 'Raised Exceptions' enabled as a breakpoint. Uncheck it and your script should run as expected.

image.png

The NameError is being raised because unicode is not defined, but it is handled by the except clause. @ShaunWalbridge mentioned, that try statement is likely unnecessary at this point. However, it isn't actually causing any problems with your script.

Tags (2)
ShawnLanning
Emerging Contributor

Thank you @NatKrase and @ShaunWalbridge.

0 Kudos
Alex_Rodriguez
Emerging Contributor

I'm having the same problem.  I unchecked the "Raised Exceptions" and "User Uncaught Exceptions" in the breakpoints section.  So now I don't get an error at all...but the script still isn't running.  The script, in its entirety is as follows.  I get nothing at all.  I AM able to log on to the server in a browser so I know ID and PW are correct and that the server is running.

 

==== code follows ==================

currSVR    = <servername>
print("gonna connect to: {0}".format(currSVR))
 
try:
    gis = GIS(currSVR, username=adminID, password=adminPW, verify_cert=False)
    print("Successfully logged in as: " + gis.properties.user.username)
except Exception as error:
    print("Nope, couldn't connect. {0}".format(error))


==== code end ==================

0 Kudos
NatKrase
New Contributor

@Alex_Rodriguez, you said that is the script "in its entirety". The script you posted doesn't import arcpy, so it really may not be doing anything.
My apologies if I read too much into that statement.

0 Kudos