Select to view content in your preferred language

Running a .pyt throws ERROR 001683: Found Python 2 to 3 errors for syntax that does not cause an error on syntax check, or other python environments

980
11
Jump to solution
05-29-2024 01:39 PM
gis_bCapell
Occasional Contributor

I am developing .pyt Python Tools with pre-existing code that I am porting into the .pyt format.

The Python code works when scripts are executed within Custom Toolboxes (.atbx), and as standalone python scripts from command prompt or my IDE (PyCharm). When I include some custom classes and functions in the .pyt from the scripts and run I get this error: ERROR 001683: Found Python 2 to 3 errors. Below is the error message and concise reproducible .pyt examples of the type of issue demonstrating error and success (following recommended edit).

I also tested the relevant error and success snippets in an .ipynb where both return the same output. Why does the .pyt error out from this code difference when the .ipynb and other Python environments do not?

gis_bCapell_1-1717014798418.png

 

 

success .pyt:

# -*- coding: utf-8 -*-

import arcpy


class Toolbox:
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""
self.label = "Toolbox"
self.alias = "toolbox"

# List of tool classes associated with this toolbox
self.tools = [Tool]

class Tool:
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "success"
self.description = ""

def getParameterInfo(self):
"""Define the tool parameters."""
params = []
return params

def isLicensed(self):
"""Set whether the tool is licensed to execute."""
return True

def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
return

def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return

def execute(self, parameters, messages):
"""The source code of the tool."""
main()
return

def postExecute(self, parameters):
"""This method takes place after outputs are processed and
added to the display."""
return

d_test = {
'1':1,
'2':2
}


def main():
for item in list(d_test.values()): #line not causing error
print(item)
arcpy.AddMessage(str(item))

error .pyt:

# -*- coding: utf-8 -*-

import arcpy


class Toolbox:
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""
self.label = "Toolbox"
self.alias = "toolbox"

# List of tool classes associated with this toolbox
self.tools = [Tool]

class Tool:
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "error"
self.description = ""

def getParameterInfo(self):
"""Define the tool parameters."""
params = []
return params

def isLicensed(self):
"""Set whether the tool is licensed to execute."""
return True

def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
return

def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return

def execute(self, parameters, messages):
"""The source code of the tool."""
main()
return

def postExecute(self, parameters):
"""This method takes place after outputs are processed and
added to the display."""
return

d_test = {
'1':1,
'2':2
}


def main():
for item in d_test.values(): #line causing error
print(item)
arcpy.AddMessage(str(item))

 

11 Replies
gis_bCapell
Occasional Contributor

@HaydenWelch ,

Great job! I got to the same thing I literally just shared that setting with my coworker. It works now. Thanks for all of your help!
Disabling this setting allows the Python 2 syntax that executes without problem in Python 3 to behave as expected.
Now to the real task at hand... figuring out how to port over a lot of code into separate .pyt files so we can encrypt them and distribute internally. 

gis_bCapell_0-1717513529863.png

 

0 Kudos
HaydenWelch
Frequent Contributor

For that part, definitely take a look at the framework library I shared earlier. Use that for testing and modularizing the toolboxes, then when you push a toolbox to production, just copy all the tool classes into one PYT file with a Toolbox object at the top of the file.

 

You can't encrypt a toolbox that relies on modular tool files, but it's a lot easier to maintain and version control than one giant flat file. The flat file pyt should be the product that you build after testing is complete and should be tagged with a version number.

0 Kudos