-1

I am learning how to convert a script written in Python 2.7 for ArcGIS Desktop 10.5 to Python 3.x so that it is compatible with ArcGIS Pro 2.3.

I used Analyze Tools For Pro to check the existing script and it seemed to fail at import StringIO so I changed it to from io import StringIO. Now it passes Analyze Tools for Pro but when I run the script in Pro I get the following error:

File "<string>", line 166 layer.visible = True ^ TabError: inconsistent use of tabs and spaces in indentation

I've tried manually fixing all of the tabs in IDLE and then saving the document, however when I reopen the "script" under the toolbox in ArcMap PRO, and it opens in IDLE, the tabs are back to five spaces.

#-------------Structures Map---------------|
arcpy.AddMessage("Generating structures map...")

newLyr = arcpy.mapping.ListLayers(mxd, "Subject Property", df)[0]
arcpy.mapping.MoveLayer(df, parcels, newLyr, "AFTER") #set the drawing order

layers = arcpy.mapping.ListLayers(mxd, "", df)
for layer in layers:
     layer.visible = False
     if layer.name in visibleStructuresLayers:
        layer.visible = True

closed as off-topic by PolyGeo Jul 24 at 20:20

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Indentation error is not specific to arcpy, it is a pure python error. Some of the indentations in your script are spaces, and some are Tabs. Depending on the software you are using to create the script you may be able to flag these, or try doing a search/replace. – smiller Jul 24 at 17:02
  • 1
    Note: mxds don't exist in ArcGIS Pro, and have been replaced by aprx files. pro.arcgis.com/en/pro-app/arcpy/mapping/… – smiller Jul 24 at 17:06
  • 1
    Please provide a code snippet that illustrates what you have tried and where you are stuck rather than a wall of code. – PolyGeo Jul 24 at 20:19
5

The indentation errors are unrelated to the name of the mapping module. As @smiller comented, you are probably mixing tabs and spaces. If you want to make sure, download Notepad++ and open your Python file by right clicking it and selecting Edit with Notepad++.

Once Notepad++ is open, click on the Show All Characters button enter image description here

It will show all special characters in your code. Spaces are represented by a yellow dot while tabs are represented by a yellow arrow. I pasted a portion of your code and replaced the 4 spaces with one tab so you could visualize it:

enter image description here

Make sure your indentation is consistent. Furthermore, what about this block of your code in the refresh template section? There is one line with one indentation more than it should have.

for lyr in arcpy.mapping.ListLayers(mxd, "", df): #set visibility and remove older layers
    lyr.visible = False
    if lyr.name == "Subject Tract":
            arcpy.mapping.RemoveLayer(df, lyr)  # THIS LINE
    if lyr.name == "Subject Property":
        arcpy.mapping.RemoveLayer(df, lyr)
    if lyr.name in visibleLocationLayers:
        lyr.visible = True


EDIT:

It looks like lines of your code use 5 spaces instead of 4 spaces of indentation. For example this block:

for layer in layers:
     if layer.name in visibleStructuresLayers:   # 5 spaces instead of 4
        layer.visible = True

Not sure if your actual code has those issues or if it was a problem of formatting the code in your question.

  • Strange, I copied the code from Sublime text and pasted it into IDLE via a toolbox in ArcMap Pro. Sure enough, when I edit the code in IDLE, a tab is 5 spaces for some reason? So I have to go through and use the space bar to manually fix all of the indentations? What makes it stranger is that this script ran fine with ArcGIS Desktop so I don't know why the formatting would change. – Anthony Stokes Jul 24 at 17:45
  • I've tried manually fixing all of the tabs in IDLE and then saving the document, however when I reopen the "script" under the toolbox in ArcMap PRO, and it opens in IDLE, the tabs are back to five spaces. – Anthony Stokes Jul 24 at 18:12
  • Many IDEs will transparently place spaces instead of a tab when you hit the TAB button. It seems to be considered best practice to use spaces instead of tabs, since spaces are the same on any system, tabs can be different. This is a convenience feature. Here is a SE answer that describes how you can fix this with Notepad++: stackoverflow.com/a/7471232/6461649 – PhilippNagel Jul 24 at 19:02
  • @AnthonyStokes It is indeed a strange situation. I haven't experienced anything similar before and could not find much on a quick search in google.I would start trying writing small blocks of code and test those on ArcGIS Pro. Could it be that you have a configuration in Sublime where one tab is 5 spaces? I would also try to run the script from a command line or a console outside of ArcGIS Pro and check if it runs or if it is still showing indentation errors. One last option would be editing the code in Notepad++ as I showed you and make sure you have 4 spaces everywhere. – Marcelo Villa Jul 24 at 19:04
  • @AnthonyStokes You can also use something like Atom, PyCharm or VSCode with a linter that highlights indentation errors for you. – Marcelo Villa Jul 24 at 19:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.