Select to view content in your preferred language

Package Project changes variables - ESRI_variable

297
4
08-23-2024 05:01 AM
TorbjørnDalløkken2
Frequent Contributor

Hi.

I have created a toolbox (.atbx) which contains several pythonscripts, which I'm using in a project template. The projects created from the project template has been working fine, and all the tools runs ok. But this week I did some refactoring in a script and created a new project template. The script is tested and runs ok. But it doesn't work in projects based on the new project template.

In my script I have this code 

def beregnKortesteVeg(self, punktid, startpunktid, tilknytningspunkter, traceLayer):
    fl_tilknytningslinje = r"memory/Tilknytningslinje"
    fl_points = "points"
    tmp_featurelayer = "tmp_featurelayer"
    
    str_punktid = f"({punktid},{startpunktid})"
    
    arcpy.SelectLayerByAttribute_management(in_layer_or_view=tilknytningspunkter,
                                                    selection_type="NEW_SELECTION",
                                                    where_clause=f'"OBJECTID" IN {str_punktid}')

When looking at the script after the packaging of the project, str_punktid and the where_clause has changed. The code has been subsituted with several ESRI_variables:

g_ESRI_variable_4 = '('
g_ESRI_variable_5 = '"OBJECTID" IN '

def beregnKortesteVeg(self, punktid, startpunktid, tilknytningspunkter, traceLayer):
        fl_tilknytningslinje = r"memory/Tilknytningslinje"
        fl_points = g_ESRI_variable_2
        tmp_featurelayer = g_ESRI_variable_3
        
        str_punktid = g_ESRI_variable_4punktid},{startpunktid})"
        
        arcpy.SelectLayerByAttribute_management(in_layer_or_view=tilknytningspunkter,
                                                        selection_type="NEW_SELECTION",
                                                        where_clause=g_ESRI_variable_5str_punktid}')

These variables (str_punktid and the where_clause) has been the same in all versions of the script and has not changed. Last time I created a working project templated was with ArcGIS Pro 3.2.x, and now I've upgraded to 3.3. In the last version (3.2) these variables was not replaced with ESRI_variables.

Are there any changes to this in 3.3?

Has anyone any resources on what the packaging does and why some variables are replaced with ESRI_variables?

 

 

 

4 Replies
MatthewDriscoll
MVP Alum

I would call ESRI support.  This seems to be a bug with the f-strings.  Or perhaps in the original code create variables for both of the f-strings like the template creation is trying to do.

0 Kudos
DavidSolari
Frequent Contributor

Huh, I've only seen this script re-writing process during web tool publishing, I guess it makes sense the same functions are used for templates.

Anyways, when you run a python script through a publishing process a preprocessor looks for potential strings that refer to the local file system and replaces them with modified paths, this is what those ESRI_VARIABLEs are. The catch is when the preprocessor messes up it can produce invalid Python as you've seen above. I bumped into some arcpy and GP tool members in San Diego and they mentioned avoiding modern format strings for now as they haven't locked the parsing down yet. You can convert f-strings like so:

 

# F-String
f"Hello {name}, you are {age:02d} years old!"
# str.format with keyword args
"Hello {name}, you are {age:02d} years old!".format(name=name, age=age)
#str.format with positional args
"Hello {}, you are {:02d} years old!".format(name, age)

 

Your specific case might be fixed with a 3.3 upgrade but it might not, if your org needs a reason to upgrade you might as well chuck this on the pile.

TorbjørnDalløkken2
Frequent Contributor

Thanks for the reply.

I think the issue is related to the tool "Consolidate Toolbox" which is run when publishing both web tool and projecttemplates, since I get the same error running that tool. The issue started after upgrading to 3.3, and it works fine with 3.2 and f-formating. With 3.2, none of these variables are replaced by g_ESRI_variables. I've also tried the other kind of formating, with the same result.

0 Kudos
TorbjørnDalløkken2
Frequent Contributor

@DavidSolari  @MatthewDriscoll 

I've done some more testing on this issue, and I found a workaround. In ArcGIS Pro 3.3 we need to use ordinary concatination and not f-string, even though f-formating worked ok in ArcGIS Pro 3.2.

I was using a f-string in a where_clause for SelectLayerByAttribute, and in my first test I tried changing this into a new variable with f-string. When running the tool, the variable str_punktid was ok but the clause was wrong (see the picture) 

TorbjrnDallkken2_0-1724746106049.png

 

When changing to ordinary string concatination, it works fine:

TorbjrnDallkken2_1-1724746276854.png

 

0 Kudos