Run standalone python script outside Arc Pro 2.9

1044
6
11-17-2022 02:54 AM
DennisWarnat90
New Contributor

I just want to run a py script standalone e.g. as a batchfile outside of pro.

Exported a model to py script. Doesn´t run outside of Pro. Adjusted File paths but it didn´t run anyway.

No python Pro. (Use IDLE to run script - no error message but no feature class was created)

py.JPG

 

0 Kudos
6 Replies
DanPatterson
MVP Esteemed Contributor

Could you copy the code to make it readable

Code formatting ... the Community Version - Esri Community


... sort of retired...
0 Kudos
DennisWarnat90
New Contributor

# -*- coding: utf-8 -*-
"""
Generated by ArcGIS ModelBuilder on : 2022-11-14 15:41:07
"""
import arcpy

def Anno_FIM(): # Anno_FIM

# To allow overwriting outputs change overwriteOutput option to True.
arcpy.env.overwriteOutput = True

Eingabekarte = "G:\\Projekte\\Test_GIS_Projekte\\Friedhofskataster\\FIM_WebOffice\\Friedhofskataster.mapx"
FIM_2022_gdb_2_ = "G:\\Projekte\\Friedhofskataster\\FIM_2022\\FIM_2022.gdb"
Beschriftung_Urnengraeber_anonym_2_ = "G:\\Projekte\\Test_GIS_Projekte\\Friedhofskataster\\FIM_WebOffice\\Beschriftung Urnengraeber.lyrx"
directory2 = "G:\\Projekte\\Friedhofskataster\\FIM_2022\\FIM_2022.gdb"

# Process: Beschriftungen in Annotation konvertieren (Beschriftungen in Annotation konvertieren) (cartography)
Beschriftung_Urnengraeber_anonym = directory2 + "Beschriftung Urnengraeber_anonym"
FIM_2022_gdb = arcpy.cartography.ConvertLabelsToAnnotation(input_map=Eingabekarte,
conversion_scale=100,
output_geodatabase=FIM_2022_gdb_2_,
anno_suffix="Annotation", extent="DEFAULT",
generate_unplaced="ONLY_PLACED",
require_symbol_id="NO_REQUIRE_ID",
feature_linked="STANDARD",
auto_create="AUTO_CREATE",
update_on_shape_change="SHAPE_UPDATE",
output_group_layer=Beschriftung_Urnengraeber_anonym,
which_layers="SINGLE_LAYER",
single_layer=Beschriftung_Urnengraeber_anonym_2_,
multiple_feature_classes="FEATURE_CLASS_PER_FEATURE_LAYER",
merge_label_classes="NO_MERGE_LABEL_CLASS")[0]

if __name__ == '__main__':
# Global Environment settings
with arcpy.EnvManager(scratchWorkspace=r"G:\Projekte\Test_GIS_Projekte\Friedhofskataster\FIM_WebOffice\FIM_WebOffice.gdb",
workspace=r"G:\Projekte\Test_GIS_Projekte\Friedhofskataster\FIM_WebOffice\FIM_WebOffice.gdb"):
Anno_FIM()

try:
input("Press enter to continue")
except SyntaxError:
input("SythaxError")

 

 

 

And this one was created by the model builder:

# -*- coding: utf-8 -*-
"""
Generated by ArcGIS ModelBuilder on : 2022-11-17 12:10:14
"""
import arcpy

def Anno_FIM(): # Anno_FIM

# To allow overwriting outputs change overwriteOutput option to True.
arcpy.env.overwriteOutput = False

Eingabekarte = "Friedhofskataster"
FIM_2022_gdb_2_ = "G:\\Projekte\\Friedhofskataster\\FIM_2022\\FIM_2022.gdb"
Beschriftung_Urnengraeber_anonym_2_ = "Beschriftung Graeber\\Beschriftung Urnengraeber_anonym"

# Process: Beschriftungen in Annotation konvertieren (Beschriftungen in Annotation konvertieren) (cartography)
Beschriftung_Urnengraeber_anonym = "Beschriftung Urnengraeber_anonym"
FIM_2022_gdb = arcpy.cartography.ConvertLabelsToAnnotation(input_map=Eingabekarte, conversion_scale=100, output_geodatabase=FIM_2022_gdb_2_, anno_suffix="Annotation", extent="DEFAULT", generate_unplaced="ONLY_PLACED", require_symbol_id="NO_REQUIRE_ID", feature_linked="STANDARD", auto_create="AUTO_CREATE", update_on_shape_change="SHAPE_UPDATE", output_group_layer=Beschriftung_Urnengraeber_anonym, which_layers="SINGLE_LAYER", single_layer=Beschriftung_Urnengraeber_anonym_2_, multiple_feature_classes="FEATURE_CLASS_PER_FEATURE_LAYER", merge_label_classes="NO_MERGE_LABEL_CLASS")[0]

if __name__ == '__main__':
# Global Environment settings
with arcpy.EnvManager(scratchWorkspace=r"G:\Projekte\Test_GIS_Projekte\Friedhofskataster\FIM_WebOffice\FIM_WebOffice.gdb", workspace=r"G:\Projekte\Test_GIS_Projekte\Friedhofskataster\FIM_WebOffice\FIM_WebOffice.gdb"):
Anno_FIM()

0 Kudos
graeme_hill
New Contributor III

It's not clear which Python environment you are running the script under in your bat file. You are using arcpy which is a licenced python module, so the script can only run with arcgis pro installed and the right pro python activated (or arcgis enterprise), check out this page to make sure your bat file is setup correctly Run stand-alone scripts—ArcGIS Pro | Documentation 

When creating posts there is an option to add text as script this is under expand toolbar > Insert/edit code sample, just makes it easier for people to read. You can also run your bat file in the cmd window (navigate to the bat file location in cmd) that way you can see error messages and post them here, you'll get more help that way.

Cheers, Graeme
MarkReddick
New Contributor III

@DennisWarnat90 I'm wondering if you ever got this working with SINGLE_LAYER?

I am doing something very similar, having published a python toolbox as a Geoprocessing service. The service is running perfectly fine and all the other functions of it work as expected (exporting data from the map etc). One component that is not working is the same example you posted which is the call to ConvertLabelsToAnnotation.

From what I can tell this does not appear to work when run within the context of a geoprocessing service (or standalone in your case).

From within Pro and using the same aprx file I can execute python commands to generate successfully with the SINGLE_LAYER option, however when run from within a SOC process as a geoprocessing service the tool generates this message (which i suspect is what you might also have seen if you added some debug output via arcpy.AddMessage(arcpy.GetMessages())):

 Start Time: Thursday, July 27, 2023 9:46:14 AM WARNING 002847: No labels to convert. There are no feature layers that are labeled at the conversion scale. Succeeded at Thursday, July 27, 2023 9:46:16 AM (Elapsed Time: 2.25 seconds)

The layer in question is visible, has labelling turned on and has no scale restriction value that impedes the generation of a label at the map scale I am asking (and which works perfectly with the exact same values when run from within Pro). 

When ALL_LAYERS is used for 'which_layers' it does generate but this is not what I need.

I have spun my wheels a lot on this, trying various things and testing in Pro and Enterprise. I feel like it is somehow tied to the fact that when opening the aprx from the Enterprise service context there is no active map (even though the aprx is saved with one). Asking for the activeMap from the ProProject gives None unlike when using from CURRENT within a Pro context. Instead I have to use listMaps()[0].

I'm probably at the point of raising a support issue but just wanted to ask here before I do.

Many thanks,

mark

 

0 Kudos
ASw93
by
Occasional Contributor

Did you figure this out? Mine won't run without actually opening the aprx files themselves. It doesn't like my "input_map" parameters.

0 Kudos
MarkReddick
New Contributor III

Just back from the weekend and catching up on some reading i see that the activeView and activeMap will always be None when run from outside of the Pro application. So that is unlikely to be the cause of the issue for the case of SINGLE_LAYER never finding any labels to convert.

0 Kudos