Python code leads to different results depending on whether it is run from the console or as a script.

12-14-2021 11:30 PM
Labels (1)
New Contributor II

Hello everybody,

I have a question related to ArcMap 10.6 and its Python API. Code I wrote leads to different results depending on whether I run it from the console or as a script.


My goal is to automate the pdf export of maps, where always the same layers are displayed. For that reason, I wish to toggle most layers not visible and just set the layers necessary to visible (all controlled by python). I created the following functions in order to reach my goal:

set_all_layers_invisible(): sets all layers in the current dataframe to not visible in a first step

set_flurkarte_visible(): sets layers defined by a provided list (group_flurkarte) to visible


def set_all_layers_invisible():
mxd_all_layers = arcpy.mapping.ListLayers(mxd, "", df)
for i in mxd_all_layers:
i.visible = False
print 'all layers set invisible'


def set_flurkarte_visible():
group_flurkarte = ["ALKIS Liegenschaftskarte Grundkarte",
"Punkte und Beschriftungen",
"Flurstücke, Lage, Punkte (Punkte und Beschriftungen)",
"Beschriftung Flurstück (DKKM)",
"Beschriftung Flurstück (DKKM) ",
"Beschriftungslinien Flurstück (DKKM)",
"Ausgestaltung Flurstück Punkte (DKKM)",
"Gebäude (Punkte und Beschriftungen)",
"Beschriftung Gebäude (DKKM)",
"Beschriftung Gebäude (DKKM) ",
"Ausgestaltung Gebäude Punkte (DKKM)",
"Bauwerke und Einrichtungen in Siedlungsflächen",
"Beschriftung Bauwerke und Einrichtungen in Siedlungsflächen (DKKM)",
"Beschriftung Bauwerke und Einrichtungen in Siedlungsflächen (DKKM) ",
"Sonstiges Bauwerk, sonstige Einrichtung Punkte",
"Linien und Flächen",
"Gebäude (Linien und Flächen)",
"Besondere Gebäudelinie",
"Flurstücke, Lage, Punkte (Linien und Flächen)",
"Besondere Flurstücksgrenze Vorschau",
"Besondere Flurstücksgrenze",
"Flurstücke (DLKM)",
"Bauwerke und Einrichtungen in Siedlungsflächen (Linien und Flächen)",
"Sonstiges Bauwerk, sonstige Einrichtung Flächen"
for i in group_flurkarte:
print len(arcpy.mapping.ListLayers(mxd, i, df))
if len(arcpy.mapping.ListLayers(mxd, i, df)) == 0:
elif len(arcpy.mapping.ListLayers(mxd, i, df)) > 1:
lyr1 = arcpy.mapping.ListLayers(mxd, i, df)[0]
lyr2 = arcpy.mapping.ListLayers(mxd, i, df)[1]
lyr1.visible = True
lyr2.visible = True
lyr = arcpy.mapping.ListLayers(mxd, i, df)[0]
lyr.visible = True


When I run these functions after another in the python console, the result is what I hoped for: All layers are set not visible at first and then all layers contained in the list group_flurkarte are set visible (see attached img1 for the result).

Eventually, the process should be integrated in a python script within a custom toolbox. But when I run the same functions in a script, just 3 layers are set visible (see attached img2 for the result).


Obviously I am doing something not as I should do it. Does someone have hints how I can solve my problem?

Thanks, Christian



The different behavier appears to be a result from different encoding handling in the console and when run as a script. Since I use special german characters (ä, ü, ö, ß) in some layer names,
these are the once not set to visible when I run the code in the script.

After adding # encoding: utf-8 to the top of the script and marking strings with special german characters as followed u"Gebäude (Punkte und Beschriftungen)".encode('utf-8'), the result when run as script still does not change.

Did anybody encounter encoding problems like that before and can give me suggestions of what to do?

0 Kudos
2 Replies
MVP Esteemed Contributor

arcmap's python is 2.7, try the expressions in ArcGIS Pro which uses python 3.7 to see if your enforced coding line has any impact

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

Thank you Dan. We are not able to use ArcGIS Pro yet.

I solved my problem with the encode method for the german language letters ä, ö and ü, for example:

u"Linien und Flächen".encode("iso-8859-1"),

For the letter ß, this solution did not work for me.