Select to view content in your preferred language

Python script running in Python window but not working as stand-alone script

1126
2
09-21-2020 02:27 AM
anyry
by
Occasional Contributor

Hello, 

I don't know why this script doesn't work. When I run it from a window in arcgis pro it works but like stand-alone script it doesn't work. It does not print any error, but no layer is selected and created in the resulting pdf. Can anyone advise me please?

import arcpy
import os
import sys
import math
from datetime import datetime

arcpy.env.overwriteOutput = True

aprx = arcpy.mp.ArcGISProject(r"C:\\Users\\aneta.ryglova\\Desktop\\layout_zk.aprx")
lyt = aprx.listLayouts("Layout1*")[0]

arcpy.env.workspace = 'C:\\Users\\aneta.ryglova\\Documents\\ArcGIS\\Projects\\MyProject\\MyProject.gdb'

klad_ztm10 = arcpy.SelectLayerByAttribute_management("klad", "NEW_SELECTION", "zm10 = '25-32-19'")

klad_ztm10_vyber = arcpy.CopyFeatures_management(klad_ztm10, "C:\\Users\\aneta.ryglova\\Documents\\ArcGIS\\Projects\\MyProject\\MyProject.gdb\\klad_ztm10")

arcpy.FeatureVerticesToPoints_management(klad_ztm10_vyber, "C:\\Users\\aneta.ryglova\\Desktop\\layout pdf\\body1.shp", "ALL")

= aprx.listMaps('Map')[0]
lyr_pryc = m.listLayers("klad_Layer*")
for layer in lyr_pryc:
    layer.visible = False

lyt.exportToPDF(r"C:\\Users\\aneta.ryglova\\Desktop\\layout pdf\\zk11.pdf", resolution = 100, image_quality = 'FASTEST')
0 Kudos
2 Replies
JoeBorgione
MVP Emeritus

The line that icludes SelectLayerByAttribute_management() is probably the whats bailing on on.  You need to Make Feature Layer (Data Management)—ArcGIS Pro | Documentation  first in a standaone script.  ArcGIS Pro is foing that for you under the hood.

(PS: if you use the syntax highlighter, we can reference line numbers in your code...)

That should just about do it....
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Joe, at some point in the early days of Pro (I don't think it was 1.x), Esri made the change where Select Layer By Attribute/Location takes data sets as well as layers/views.  When passed a data set, the tool will create a layer/view, and that behavior is the same whether run within Pro application or stand-alone script.  Personally, I have found the behavior odd, if not buggy, at times.  I don't think there was anything wrong with making the user create a layer/view first, after all, the tools are called Select Layer By Attribute/Location.

In this case, the OP is passing a layer name (not a data set name) to a layer that hasn't been created, so you are most likely correct that line is causing the issue.