How to debug arcpy code in visual studio 2019

1452
8
05-13-2021 02:41 PM
ManishShrivastav
New Contributor III
# This script clips all feature classes in a file geodatabase

import arcpy

# Create path variables
sourceWorkspace = r"C:\Users\Lesson2Practice\USA.gdb"
targetWorkspace = r"C:\Users\Lesson2Practice\Iowa.gdb"
clipfeature = r"C:\Users\Lesson2Practice\Iowa.gdb\Iowa"

# Get a list of all feature classes in the USA geodatabase
arcpy.env.workspace = sourceWorkspace
featureClassList = arcpy.ListFeatureClasses()

try:
    # Loop through all USA faeture classes
    for featureClass in featureClassList:
        
        # Construct the output path
        outClipFeatureClass = f'{targetWorkspace}\Iowa{featureClass}'

        # Perform Clip
        arcpy.Clip_analysis(featureClass,clipfeature,outClipFeatureClass)
        arcpy.AddMessage(f'Wrote clipped file {outClipFeatureClass}.')
        print(f"Wrote clipped file {outClipFeatureClass}.")
except:
    # Report if there was an error
    arcpy.AddError("Could not clip feature classes")
    print("Could not clip feature classses")
    print(arcpy.GetMessages())

I have created a python script file that I am debugging it using visual studio 2019, But as soon as I try to debug, it gives me and Exception on another file (time.py - not sure where it came from as this time.py is not in my project or folder) stating "name 'm' is not defined" (I am attaching a video of the exception). Any help is appreciated.

 

Thanks

0 Kudos
8 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

Have you solved issue with "name 'm' is not defined"?

I have the same situation as you

0 Kudos
ManishShrivastav
New Contributor III

I am still waiting for it.

0 Kudos
ChenChen4
New Contributor III

I have the same issue. It looks like when I import arcpy in Visual Studio, it loads 'arcpy.time', and it doesn't matter if I use any datetime functions in my code but the debug detects the name error.

I opened the time.py file and in line 267, there is a variable m

try:
del m
except NameError:
pass

I cannot modify this time.py file since it's part of arcpy package. Don't know how to solve this problem.

0 Kudos
RobbCampbell
New Contributor II

Did anyone Get any help on this? I have the same issue as the rest of you. ChenChen4 is exactly where I got.

0 Kudos
BrynR
by
New Contributor III

I'm running into this same message too when adding "import arcpy"

ArcPy Debugging.png

It's annoying but is this actually a problem? I notice you can just click "Continue" on the visual studio toolbar and the rest of my project runs fine.

Also, if I choose to "Debug"-> "Execute Project in Python Interactive" there's no error at all.

0 Kudos
GKmieliauskas
Esri Regular Contributor

Hi,

When you  wrote "if I choose to "Debug"-> "Execute Project in Python Interactive" there's no error at all.", do you mean running script with $load command?

For me it stops with other things like:

dirname = os.path.dirname(__file__)

 

0 Kudos
BrynR
by
New Contributor III

No, I mean the the option from the Debug menu. Shortcut key Shift+Alt+F5

Capture.JPG

ACrateau
New Contributor III

Did anyone ever solve this?

I am trying to debug a project in VS using the arcgispro-py3 environment.  This error happens when you reference python's datetime.datetime class after the arcpy site package is loaded.

The project works when run in the python interactive window, but throws the same error when using Debug in VS.