<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Python code leads to different results depending on whether it is run from the console or as a script. in ArcMap Questions</title>
    <link>https://community.esri.com/t5/arcmap-questions/python-code-leads-to-different-results-depending/m-p/1152463#M3544</link>
    <description>&lt;P&gt;Thank you Dan. We are not able to use ArcGIS Pro yet.&lt;/P&gt;&lt;P&gt;I solved my problem with the encode method for the german language letters ä, ö and ü, for example:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;u"Linien und Flächen".encode("iso-8859-1"),&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;For the letter ß, this solution did not work for me.&lt;/P&gt;&lt;P&gt;Christian&lt;/P&gt;</description>
    <pubDate>Thu, 10 Mar 2022 09:20:52 GMT</pubDate>
    <dc:creator>ChristianJungnickl</dc:creator>
    <dc:date>2022-03-10T09:20:52Z</dc:date>
    <item>
      <title>Python code leads to different results depending on whether it is run from the console or as a script.</title>
      <link>https://community.esri.com/t5/arcmap-questions/python-code-leads-to-different-results-depending/m-p/1126177#M3269</link>
      <description>&lt;P&gt;Hello everybody,&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;set_all_layers_invisible(): sets all layers in the current dataframe to not visible in a first step&lt;/P&gt;&lt;P&gt;set_flurkarte_visible(): sets layers defined by a provided list (group_flurkarte) to visible&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#808080"&gt;def set_all_layers_invisible():&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#808080"&gt;mxd_all_layers = arcpy.mapping.ListLayers(mxd, "", df)&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#808080"&gt;for i in mxd_all_layers:&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#808080"&gt;try:&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#808080"&gt;i.visible = False&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#808080"&gt;except:&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#808080"&gt;pass&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#808080"&gt;arcpy.RefreshActiveView()&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#808080"&gt;print 'all layers set invisible'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#808080"&gt;def set_flurkarte_visible():&lt;BR /&gt;group_flurkarte = ["ALKIS Liegenschaftskarte Grundkarte",&lt;BR /&gt;"Punkte und Beschriftungen",&lt;BR /&gt;"Flurstücke, Lage, Punkte (Punkte und Beschriftungen)",&lt;BR /&gt;"Beschriftung Flurstück (DKKM)",&lt;BR /&gt;"Beschriftung Flurstück (DKKM) ",&lt;BR /&gt;"Beschriftungslinien Flurstück (DKKM)",&lt;BR /&gt;"Ausgestaltung Flurstück Punkte (DKKM)",&lt;BR /&gt;"Gebäude (Punkte und Beschriftungen)",&lt;BR /&gt;"Beschriftung Gebäude (DKKM)",&lt;BR /&gt;"Beschriftung Gebäude (DKKM) ",&lt;BR /&gt;"Ausgestaltung Gebäude Punkte (DKKM)",&lt;BR /&gt;"Bauwerke und Einrichtungen in Siedlungsflächen",&lt;BR /&gt;"Beschriftung Bauwerke und Einrichtungen in Siedlungsflächen (DKKM)",&lt;BR /&gt;"Beschriftung Bauwerke und Einrichtungen in Siedlungsflächen (DKKM) ",&lt;BR /&gt;"Sonstiges Bauwerk, sonstige Einrichtung Punkte",&lt;BR /&gt;"Linien und Flächen",&lt;BR /&gt;"Gebäude (Linien und Flächen)",&lt;BR /&gt;"Firstlinie",&lt;BR /&gt;"Besondere Gebäudelinie",&lt;BR /&gt;"Bauteil",&lt;BR /&gt;"Gebäude",&lt;BR /&gt;"Flurstücke, Lage, Punkte (Linien und Flächen)",&lt;BR /&gt;"Besondere Flurstücksgrenze Vorschau",&lt;BR /&gt;"Besondere Flurstücksgrenze",&lt;BR /&gt;"Flurstücke (DLKM)",&lt;BR /&gt;"Bauwerke und Einrichtungen in Siedlungsflächen (Linien und Flächen)",&lt;BR /&gt;"Sonstiges Bauwerk, sonstige Einrichtung Flächen"&lt;BR /&gt;]&lt;BR /&gt;for i in group_flurkarte:&lt;BR /&gt;print len(arcpy.mapping.ListLayers(mxd, i, df))&lt;BR /&gt;if len(arcpy.mapping.ListLayers(mxd, i, df)) == 0:&lt;BR /&gt;pass&lt;BR /&gt;elif len(arcpy.mapping.ListLayers(mxd, i, df)) &amp;gt; 1:&lt;BR /&gt;lyr1 = arcpy.mapping.ListLayers(mxd, i, df)[0]&lt;BR /&gt;lyr2 = arcpy.mapping.ListLayers(mxd, i, df)[1]&lt;BR /&gt;lyr1.visible = True&lt;BR /&gt;lyr2.visible = True&lt;BR /&gt;else:&lt;BR /&gt;lyr = arcpy.mapping.ListLayers(mxd, i, df)[0]&lt;BR /&gt;lyr.visible = True&lt;BR /&gt;arcpy.RefreshTOC()&lt;BR /&gt;arcpy.RefreshActiveView()&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;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).&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;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).&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;Obviously I am doing something not as I should do it. Does someone have hints how I can solve my problem?&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;Thanks, Christian&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;EDIT:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;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,&lt;BR /&gt;these are the once not set to visible when I run the code in the script.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;After adding &lt;FONT color="#808080"&gt;# encoding: utf-8&lt;/FONT&gt; to the top of the script and marking strings with special german characters as followed &lt;/FONT&gt;&lt;FONT color="#000000"&gt;&lt;FONT color="#808080"&gt;u"Gebäude (Punkte und Beschriftungen)".encode('utf-8'),&lt;/FONT&gt; the result when run as script still does not change.&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;Did anybody encounter encoding problems like that before and can give me suggestions of what to do?&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Dec 2021 09:22:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/python-code-leads-to-different-results-depending/m-p/1126177#M3269</guid>
      <dc:creator>ChristianJungnickl</dc:creator>
      <dc:date>2021-12-15T09:22:45Z</dc:date>
    </item>
    <item>
      <title>Re: Python code leads to different results depending on whether it is run from the console or as a script.</title>
      <link>https://community.esri.com/t5/arcmap-questions/python-code-leads-to-different-results-depending/m-p/1126224#M3270</link>
      <description>&lt;P&gt;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&lt;/P&gt;</description>
      <pubDate>Wed, 15 Dec 2021 12:38:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/python-code-leads-to-different-results-depending/m-p/1126224#M3270</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-12-15T12:38:57Z</dc:date>
    </item>
    <item>
      <title>Re: Python code leads to different results depending on whether it is run from the console or as a script.</title>
      <link>https://community.esri.com/t5/arcmap-questions/python-code-leads-to-different-results-depending/m-p/1152463#M3544</link>
      <description>&lt;P&gt;Thank you Dan. We are not able to use ArcGIS Pro yet.&lt;/P&gt;&lt;P&gt;I solved my problem with the encode method for the german language letters ä, ö and ü, for example:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;u"Linien und Flächen".encode("iso-8859-1"),&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;For the letter ß, this solution did not work for me.&lt;/P&gt;&lt;P&gt;Christian&lt;/P&gt;</description>
      <pubDate>Thu, 10 Mar 2022 09:20:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/python-code-leads-to-different-results-depending/m-p/1152463#M3544</guid>
      <dc:creator>ChristianJungnickl</dc:creator>
      <dc:date>2022-03-10T09:20:52Z</dc:date>
    </item>
  </channel>
</rss>

