Python script with project pathname does not do anything

362
1
01-06-2023 11:10 AM
chill_gis_dude
New Contributor III

I have several python scripts that I would like to have run outside of ArcGIS Pro. Its mainly to export map series weekly without having to manually open the project. I keep hitting a dead end because most of my python scripts do nothing even when ran in the project if I set arcpy.mp.ArcGISProject to reference the path of the project. All of my scripts work if I change the pathname to ("Current") but obviously that can't be run outside of Pro. Here are two samples of python scripts I am trying to get to work. 

##  BLOCK DETAILS TO BLOCK NAMES ##
aprx = arcpy.mp.ArcGISProject("C:\\GIS Local Directory\\RanchMaps.aprx")
for m in aprx.listMaps("Default Map"):
   lyr = m.listLayers("Block Boundary")[0]
   z = ["default"], ["details open"], ["Ranches Zoomed In"], ["Center Unlocked"], ["Center Locked"], ["Center Left Locked"]
   for i in z:
      if lyr.supports("SHOWLABELS"):
         lblClass = lyr.listLabelClasses(i[0])[0]
         lblClass.visible = False
      if lyr.supports("SHOWLABELS"):
         lblClass = lyr.listLabelClasses("block name")[0]
         lblClass.visible = True


import arcpy
aprx = arcpy.mp.ArcGISProject("C:\\GIS Local Directory\\RanchMaps.aprx")
m = aprx.listMaps("Default Map")[0]
lyrList = m.listLayers()
for lyr in lyrList:
  if lyr.longName == "Ranches (Sun Pacific)\\Block Details":
    lyr.visible = False
  if lyr.longName == "Ranches (Sun Pacific)\\Block Names":
    lyr.visible = True
aprx.save()
del aprx

 

Again they both work as intended if I change the project path name to ("CURRENT")

 

Thanks for any help

 

 

 

0 Kudos
1 Reply
DuncanHornby
MVP Notable Contributor

The following bit of code worked for me, I ran it from the IDE spyder:

import arcpy
aprx = arcpy.mp.ArcGISProject(r"C:\Scratch\test\test.aprx")
m = aprx.listMaps("Map3")[0]
lyrList = m.listLayers()
for lyr in lyrList:
    if lyr.longName == "Source Nodes":
        lyr.visible = True
aprx.save()
print("Done!")

 

You should not have ArcPro open as that will surely file lock the project. How are you running your external scripts? Are you running them from an IDE or are they scheduled by Windows? I think the source of your problem lies elsewhere not in the code itself?

0 Kudos