Select to view content in your preferred language

OSError: Script tool fails when ran from another PC/instance of Pro

670
9
09-13-2024 01:36 PM
JaredPilbeam2
MVP Regular Contributor

I have a simple custom script tool that I want to share within our GIS office. The tool works fine from in my Pro application. Both the tool and the stand alone script it points to are stored in a network folder/UNC path that everyone has access to. When ran from another instance of Pro, whether from my colleague's PC or a dedicated box I remote log into, the tool throws this error:

 Traceback (most recent call last):
  File "\\uncpathtothescript\LayoutToPDF_Tool.py", line 26, in <module>
    current_aprx = arcpy.mp.ArcGISProject(os.path.join(env, file))
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\_mp.py", line 337, in __init__
    self._arc_object = arcgisscripting._mapping.ArcGISProject(*gp_fixargs((aprx_path,), True))
OSError: \\uncpathtoproproject\COUNTY_BOARD_A.aprx
 Failed to execute (LayoutToPDFs).

I haven't debugged other than running the tool on different machines. I've read you can share to Enterprise Portal, but I'm not interested in that. If I can simply share to a network folder so anyone can just simply open it immediately from their system that would be best.

The tool exports layouts to PDFs.

#-------------------------------------------------------------------------------
# Name:        LayoutToPDF_Tool.py
# Purpose:     Exports APRX layout to PDF.
#
# Author:      jpilbeam
#
# Created:     13/08/2024
# Copyright:   (c) Will Co GIS
# Licence:     <WillCo GIS>
#-------------------------------------------------------------------------------

import arcpy, os

#workspace folder
env = arcpy.env.workspace = arcpy.GetParameterAsText(0)
#print the number of aprx files in the workspace folder
aprxlist = arcpy.ListFiles("*.aprx")
arcpy.AddMessage("Currently {0} maps to be updated:\n".format(len(aprxlist)))

#Loop through folder and list APRXs
for dirName, subdirList, fileList in os.walk(env):
    for file in fileList:
        if file.endswith(".aprx"):
            current_aprx = arcpy.mp.ArcGISProject(os.path.join(env, file))
                       
            for lyt in current_aprx.listLayouts():
                #export APRXs to PDFs
                lyt.exportToPDF(f'{env}\{lyt.name}')
                arcpy.AddMessage("{} to PDF".format(lyt.name))

# current_aprx.save()
del current_aprx
print("----done----")
0 Kudos
9 Replies
HaydenWelch
Frequent Contributor

Have you tried formatting the path using pathlib? Whatever is happening seems to be related to the UNC path, and pathlib has some more tools for dealing with paths that aren't just drive letters and directories.

Could also be a user permission error?

0 Kudos
JakeSkinner
Esri Esteemed Contributor

@JaredPilbeam2 does the tool fail from the client machine when you specify the workspace folder with a drive letter that is local/mapped to that same client machine?

0 Kudos
JaredPilbeam2
MVP Regular Contributor

@JakeSkinnerYes. I just ran the tool with a test folder as a workspace on the remote machine's C drive. The result was the same OSError.

I'm looking into pathlib. Thanks for the suggestion @HaydenWelch 

0 Kudos
JaredPilbeam2
MVP Regular Contributor

Unless I didn't use Path properly, the script tool failed with the same OSError on the same line as before.

from pathlib import Path

#workspace folder
arcpy.env.workspace = arcpy.GetParameterAsText(0)
env = Path(arcpy.env.workspace)

 

line 26 (the line it's been failing on):

current_aprx = arcpy.mp.ArcGISProject(os.path.join(env, file))

 

0 Kudos
HaydenWelch
Frequent Contributor

You're combining os.path and pathlib, you can join a path inline with pathlib like so:

from pathlib import Path

#workspace folder
arcpy.env.workspace = arcpy.GetParameterAsText(0)
env = Path(arcpy.env.workspace)
...
current_aprx = arcpy.mp.ArcGISProject(env / file)
# Or with a methodcall
current_aprx = arcpy.mp.ArcGISProject(env.joinpath(file))

The only reason I'm suggesting pathlib is because it gives a lot more control over the your paths. You can resolve it to posix or uri formats. If you're using a UNC path format you might need to play around with it. Pathlib will normalize the input string for you, but I think using joinpath or / will skip normalization.

Whatever is happening it seems like you're generating a valid UNC path that the os.path and pathlib modules like, but are feeding it in a weird format to the ArcGISProject function.

0 Kudos
JaredPilbeam2
MVP Regular Contributor

@JakeSkinnerI slightly misunderstood your reply the first time. I just tried running the tool from the remote box again. This time in the workspace parameter I used a mapped letter drive. It still resulted in the same OSError.

0 Kudos
JakeSkinner
Esri Esteemed Contributor

@JaredPilbeam2 are you able to open the Pro Project and save changes to it when logged into the remote server?

0 Kudos
JaredPilbeam2
MVP Regular Contributor

It's not a remote server, just a remote computer. I'm just using the catalog pane in a random Pro project while testing as I'm sure my colleagues will be opening random projects in the future to use the tool. But, to answer you question: Yes, I can open and save the same Pro project on the remote computer as well as mine.

I noticed my tools were in .tbx format. I'm going to create an .atbx and transfer the tool(s) over then  try again.

0 Kudos
JaredPilbeam2
MVP Regular Contributor

That seems to have solved the problem, at least on the remote computer. I also updated Pro to the latest (3.3.1) on that machine. It completed with no error. However, then I had a colleague run the tool and it still throws an OSError on his machine. Now the error is on a different line, only:

lyt.exportToPDF(f'{env}\{lyt.name}')
0 Kudos